---
title: "Samson data cleaning S21"
authors: "S. Shin, C. Holland"
date: "4/30/2021"
output: html_document
---
#Load Packages
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(haven)
library(stringr)
library(rstatix)
library(sjlabelled)
library(tidyverse)
```


#Load Classifications
```{r}
#Read in Coding information for all studies
classifications <- read.csv("../data_files/Samson_Classifications.csv")
```

#Data Cleaning (S. Shin)

##Qureshi 2018
```{r}
Qureshi_2018_ER <- read.csv('../data_files/Qureshi_2018_ER.csv')
Qureshi_2018_RT <- read.csv('../data_files/Qureshi_2018_RT.csv')

#Error rate data
Qureshi_2018_ER <- Qureshi_2018_ER %>% 
  rowid_to_column('Subject') %>% #add subject label
  gather(key = Condition, value = Value, Alone_COM_ErrRate:Dual_ISM_ErrRate) %>% #change from wide to long
  mutate( 
    RTorError = "Error", #add necessary columns to fit format
    Consistency = case_when(grepl("C", Condition)~"con", grepl("I", Condition)~"incon"),
    SelfOther = case_when(grepl("S", Condition)~"self", grepl("O", Condition)~"other"),
    Subject = paste("Qureshi_2018_", Subject),
    Paper = "Qureshi_2018",
    StudyNumber = 1,
    Age = NA,
    Sex = NA,
    Matching = 'match')

#Response time data
Qureshi_2018_RT["Subject"] = 55:108 #add subject number
Qureshi_2018_RT <- Qureshi_2018_RT %>%
  gather(key = Condition, value = Value, Alone_COM_RT:Dual_ISM_RT) %>% 
  mutate(
    RTorError = "RT",
    Consistency = case_when(grepl("C", Condition)~"con", grepl("I", Condition)~"incon"),
    SelfOther = case_when(grepl("S", Condition)~"self", grepl("O", Condition)~"other"),
    Subject = paste("Qureshi_2018_", Subject),
    Paper = "Qureshi_2018",
    StudyNumber = 1,
    Age = NA,
    Sex = NA,
    Matching = 'match')

#combine ER and RT
Qureshi_2018 <- rbind(Qureshi_2018_RT, Qureshi_2018_ER)

#merge with coding info
Qureshi_2018_Codes <- filter(classifications, Paper == "Qureshi_2018")

#merge
Qureshi_2018 <- inner_join(Qureshi_2018_Codes, Qureshi_2018)
```


##Qureshi 2020
```{r}
Qureshi_2020_ER <- read.csv('../data_files/Qureshi_2020_ER.csv')
Qureshi_2020_RT <- read.csv('../data_files/Qureshi_2020_RT.csv')

#change from wide to long
Qureshi_2020_ER <- Qureshi_2020_ER %>% 
  rowid_to_column('Subject') %>% #add subject column
  gather(key = Condition, value = Value, COM:ISMM) %>% #wide to long
  mutate(
    Matching = case_when(str_length(Condition) == 3 ~ 'match', str_length(Condition) == 4 ~ "mismatch"), #record matching or mismatching
    Value = Value / 96, #calculate ER from number of errors (96 trials)
    RTorError = "Error",
    Subject = paste('Qureshi_2020_', Subject),
    Paper = "Qureshi_2020",
    StudyNumber = 1,
    Age = NA,
    Sex = NA)

Qureshi_2020_RT <- Qureshi_2020_RT %>%
  rowid_to_column('Subject') %>% #add subject column (author confirmed subjects match up by row)
  gather(key = Condition, value = Value, COM:ISMM) %>% #wide to long
  mutate(
    Matching = case_when(str_length(Condition) == 3 ~ 'match', str_length(Condition) == 4 ~ 'mismatch'), #record matching  (1) or mismatching (0)
    RTorError = "RT",
    Subject = paste('Qureshi_2020_', Subject),
    Paper = "Qureshi_2020",
    StudyNumber = 1,
    Age = NA,
    Sex = NA)

#combine er and rt
Qureshi_2020 <- rbind(Qureshi_2020_RT, Qureshi_2020_ER)

Qureshi_2020 <- Qureshi_2020 %>% 
  mutate(
    Consistency = case_when(grepl("C", Condition)~"con", grepl("I", Condition)~"incon"),
    SelfOther = case_when(grepl("S", Condition)~"self", grepl("O", Condition)~"other"),
    Condition = paste("Qureshi_2020", Condition, sep = "_")
  )

#merge with coding df
Qureshi_2020_Codes <- filter(classifications, Paper == "Qureshi_2020")

Qureshi_2020 <- inner_join(Qureshi_2020_Codes, Qureshi_2020)
```


##Drayton 2018
```{r}
Drayton_2018_data <- read.table("../data_files/Drayton_2018.dat", header = TRUE, skip = 0)

#convert data from wide to long format
Drayton_2018_long <- Drayton_2018_data %>% 
  select(-Altercentric, -Egocentric, -totaltrials, -ErrorRate, -ExcludeRate) %>% #get rid of extraneous columns
  gather(key = Condition, value = Value, OwnCon_ERROR:OtherInc_RT)#wide to long

#add relevant columns
Drayton_2018 <- Drayton_2018_long %>% 
  mutate(
    RTorError = case_when(substr(Condition, nchar(Condition), nchar(Condition)) == "T" ~ "RT", substr(Condition, nchar(Condition), nchar(Condition)) == "R" ~ "Error"), #record measure 
    Subject = paste("Drayton_2018", subject, sep = "_"), #label subjects
    Paper = "Drayton_2018",
    StudyNumber = 1,
    Age = NA,
    Sex = "M", #all male subjects
    Matching = 'mixed',
    Consistency = case_when(grepl("Con", Condition)~"con", grepl("Inc", Condition)~"incon"),
    SelfOther = case_when(grepl("Own", Condition)~"self", grepl("Other", Condition)~"other"),
    Value= case_when(RTorError=="RT"~Value*100, TRUE~Value)#change tenth of second to millisecond
    ) %>% 
  select(-subject) #get rid of outdated subject column


#merge with coding df
Drayton_2018_Codes <- filter(classifications, Paper == "Drayton_2018")

Drayton_2018 <- inner_join(Drayton_2018_Codes, Drayton_2018)
```

##Wang 2019
```{r}
Wang_2019 <- read.csv("../data_files/Wang_2019.csv")

#calculate average rt
Wang_2019_rt <- Wang_2019 %>% 
  filter(acc == 1) %>%  #remove incorrect trials
  group_by(subject) %>% 
  filter(rt >= (mean(rt)-2.5*sd(rt)) & rt <= (mean(rt)+2.5*sd(rt))) #filter to 2.5 SD from mean for each subject

Wang_2019_rt <- Wang_2019 %>%
  group_by(subject, persp, culture, consistency) %>% #change to condition x participant grouping
  summarise(rt = mean(rt)) %>%  #calculate mean value 
  mutate(RTorError = "RT") %>% 
  rename(Value = rt)

#calculate average er and convert column names
Wang_2019_er <- Wang_2019 %>% 
  group_by(subject, persp, culture, consistency) %>% #condition x participant grouping
  summarise(acc = mean(acc)) %>% 
  mutate(
    RTorError = "Error", 
    acc = 1-acc) %>% #converty acc to er
  rename(Value = acc)

#combine er and rt averages
Wang_2019 <- rbind(Wang_2019_er, Wang_2019_rt)

#reformat and rename
Wang_2019 <- Wang_2019 %>% 
  mutate(
    Condition = paste(persp, consistency, sep = "_"),
    Subject = paste("Wang_2019", subject, culture, sep = "_"),
    Paper = "Wang_2019",
    StudyNumber = 1,
    Age = NA,
    Sex = NA,
    Matching = "mixed",
    Consistency = case_when(grepl("_con", Condition)~"con", grepl("_incon", Condition)~"incon"),
    SelfOther = case_when(grepl("self", Condition)~"self", grepl("other", Condition)~"other")) %>% 
  ungroup() %>% 
  select(-c(persp, consistency, subject, culture))


#merge with coding df
Wang_2019_Codes <- filter(classifications, Paper == "Wang_2019")

Wang_2019 <- inner_join(Wang_2019_Codes, Wang_2019)
```



##Saether 2021
```{r}
Saether_2021 <- read.csv("../data_files/Saether_2021.csv")

#drop practice trials
Saether_2021 <- Saether_2021 %>% filter(grepl("testblock", blocktype)) 

#calculate mean rt
Saether_2021_rt <- Saether_2021 %>%
  filter(accuracy == 1) %>% 
  group_by(subid) %>% 
  filter(ReactionTime >= (mean(ReactionTime)-2.5*sd(ReactionTime)) & ReactionTime <= (mean(ReactionTime)+2.5*sd(ReactionTime))) %>% #filter to 2.5 SD from mean for each subject
  group_by(subid, subage, gender, perspective, consistency, trial_type) %>% 
  summarise(Value =mean(ReactionTime)) %>% #calculate mean at condition x participant level
  mutate(
    RTorError = 'RT')

#calculate mean er
Saether_2021_er <- Saether_2021 %>%
  group_by(subid, subage, gender, perspective, consistency, trial_type) %>% 
  summarise(Value =mean(accuracy)) %>% 
  mutate(
    RTorError = 'Error')

#join means
Saether_2021 <- rbind(Saether_2021_rt, Saether_2021_er)

#modify columns to fit template
Saether_2021 <- Saether_2021 %>% 
  mutate(
    Paper = "Saether_2021",
    Condition = paste(perspective, consistency, sep = "_"),
    Subject = paste("Saether_2021", subid, sep = "_"),
    StudyNumber = 1,
    Sex = case_when(gender=="Male"~"M", gender=="Female"~"F"),
    Consistency = case_when(grepl("_con", Condition)~"con", grepl("_incon", Condition)~"incon"),
    SelfOther = case_when(grepl("self", Condition)~"self", grepl("other", Condition)~"other")) %>% 
  rename(
    Age = subage,
    Matching = trial_type
  ) %>% 
  ungroup() %>% 
  select(-perspective, -consistency, -subid, -gender) %>% 
  mutate(Value = case_when(RTorError == "Error"~1-Value, TRUE~Value)) #change acc rate to err rate

#merge with coding df
Saether_2021_Codes <- filter(classifications, Paper == "Saether_2021") 

Saether_2021 <- inner_join(Saether_2021_Codes, Saether_2021)
```

##Simonsen 2020
```{r}
Simonsen_2020 <- read.csv("../data_files/Simonsen_2020.csv")

#consolidate columns
Simonsen_2020 <- Simonsen_2020 %>%
  mutate(
    Subject = paste('Simonsen_2020', ID, sep = "_"),
    Consistency = case_when(Consistency == 1 ~ 'con', Consistency == 0 ~ 'incon'),
    Condition = paste(Self, Consistency, Group, sep = '_'),
    Matching = case_when(CorrectResponse == 'j' ~ "match", CorrectResponse == 'n' ~ "mismatch"), #j='ja' (translation 'yes') n='nej' (translation 'no')
    ) %>% #get rid of outdated columns
  select(-Response, -Number, -Picture, -Trial, -selfDisks, -otherDisks, -Consistency,-ExperimentName) #get rid of unused columns



#average rt data
Simonsen_2020_rt <- Simonsen_2020 %>%
  filter(Accuracy == 1) %>% #get rid of incorrect trials
  group_by(Subject) %>% 
  filter(RT >= (mean(RT)-2.5*sd(RT)) & RT <= (mean(RT)+2.5*sd(RT))) %>% #filter to 2.5 sd from mean for each participant
  group_by(Subject, Condition, Matching) %>% 
  summarise(Value = mean(RT)) %>% #calculate mean rt at subject x condition level
  mutate(RTorError = "RT")

#average er data
Simonsen_2020_er <- Simonsen_2020 %>%
  group_by(Subject, Condition, Matching) %>% 
  summarise(Value = mean(Accuracy)) %>% 
  mutate(RTorError = "Error") 

#combine er and rt averages
Simonsen_2020 <- rbind(Simonsen_2020_rt, Simonsen_2020_er)

#add a couple more necessary columns
Simonsen_2020 <- Simonsen_2020 %>%
  mutate(
    Paper = "Simonsen_2020",
    StudyNumber = 1,
    Consistency = case_when(
      grepl("_con_", Condition) ~ "con",
      grepl("_incon_", Condition) ~ "incon"),
    SelfOther =case_when(
      grepl("Self", Condition) ~ "self",
      grepl("Other", Condition) ~ "other"),
    Value = case_when(RTorError=="Error" ~ 1-Value, TRUE ~ Value), #change acc to err
    Age = NA,
    Sex = NA)

#merge with coding df
Simonsen_2020_Codes <- filter(classifications, Paper == "Simonsen_2020") 

Simonsen_2020 <- inner_join(Simonsen_2020_Codes, Simonsen_2020)
```

##Ramsey 2013
```{r}
Ramsey_2013 <- read.csv("../data_files/Ramsey_2013.csv")


#wide to long
Ramsey_2013 <- Ramsey_2013 %>% 
  gather(key = Subject, value = Value, s2_RT:s18_acc) %>% 
  filter(CODE != "filler") #remove fillers

#create unique participant x trial id
Ramsey_2013 <- Ramsey_2013 %>%
  mutate(
    RTorError = case_when(substr(Subject, nchar(Subject), nchar(Subject)) == "T" ~ "RT", 
                          substr(Subject, nchar(Subject), nchar(Subject)) == "c" ~ "Error"),
    Subject = str_extract(Subject, "\\d"),
    trialid = factor(paste(X, Subject, sep = "_"))
  ) %>% 
  filter( #remove invalid entries
    Value != '#VALUE!' & Value != 'timeout'
  )

#separate er and rt
Ramsey_2013_rt <- filter(Ramsey_2013, RTorError == "RT")
Ramsey_2013_er <- filter(Ramsey_2013, RTorError == "Error")


#Remove failed trials from rt data
Ramsey_er_codes <- Ramsey_2013_er %>%
  select(Value, trialid) %>% 
  rename(Correct = Value)

Ramsey_2013_rt <- bind_cols(Ramsey_2013_rt, Ramsey_er_codes)
#unique(print(Ramsey_2013_rt$trialid...7==Ramsey_2013_rt$trialid...9)) #double check trialid still matched

Ramsey_2013_rt <- Ramsey_2013_rt %>% 
  filter(Correct == 1)


#average by participant, and add in rt or err identity
Ramsey_2013_er <- Ramsey_2013_er %>% 
  group_by(CODE, Subject) %>% 
  summarize(Value = mean(as.numeric(Value))) %>% 
  mutate( RTorError = "Error")

Ramsey_2013_rt <- Ramsey_2013_rt %>% 
  group_by(Subject) %>% 
  mutate(Value = as.numeric(Value)) %>% 
  filter(Value >= (mean(Value)-2.5*sd(Value)) & Value <= (mean(Value)+2.5*sd(Value))) %>% #filter to 2.5 SD from mean for each subject
  group_by(CODE, Subject) %>% 
  summarize(Value = mean(as.numeric(Value))) %>% #calclate mean at subject x condition level
  mutate(RTorError = "RT")

Ramsey_2013 <- rbind(Ramsey_2013_er, Ramsey_2013_rt)

#change colnames and coding
Ramsey_2013 <- Ramsey_2013 %>% 
  mutate(
    Value = case_when(RTorError == "RT" ~ Value*1000, RTorError == "Error" ~ 1-Value), #chang4 sec to ms, change acc to err
    Sex = NA,
    Age = NA,
    Matching = case_when(nchar(as.character(CODE)) == 3 ~ "match", nchar(as.character(CODE)) == 4 ~ "mismatch"),
    StudyNumber = 1,
    Paper = "Ramsey_2013",
    Subject = paste("Ramsey_2013", Subject, sep = "_"),
    Consistency = case_when(
      substr(as.character(CODE),1,1)=="C" ~ "con",
      substr(as.character(CODE),1,1)=="I" ~ "incon"),
    SelfOther = case_when(
      substr(as.character(CODE),2,2)=="S" ~ "self",
      substr(as.character(CODE),2,2)=="O" ~ "other")
  ) %>% 
  rename(Condition = CODE)


#merge with coding df
Ramsey_2013_Codes <- filter(classifications, Paper == "Ramsey_2013") 

Ramsey_2013 <- inner_join(Ramsey_2013_Codes, Ramsey_2013)
```

##Pavlidou 2018
```{r}
Pavlidou_2018 <- read_csv("../data_files/Pavlidou_2018.csv")

#remove filler trials
Pavlidou_2018 <- Pavlidou_2018 %>%
  filter(fillers == 'no') %>% 
  select(-fillers)

#consolidate columns
Pavlidou_2018 <- Pavlidou_2018 %>% 
  mutate(
    Condition = paste("self", consistency, task, condition, sep = "_"),
    Subject = paste("Pavlidou_2018", subject, sep = "_"),
    StudyNumber = case_when(task =="avatar" ~ 1, task == "arrow" ~ 2),
    rt_raw = rt_raw*1000) %>%  #change sec to ms
  select(-c(consistency, task, condition, subject))

#calculate mean rt
Pavlidou_2018_rt <- Pavlidou_2018 %>% 
  filter(corr_raw == 1) %>%  #remove incorrect trials
  group_by(Subject) %>% 
  filter(rt_raw >= (mean(rt_raw)-2.5*sd(rt_raw)) & rt_raw <= (mean(rt_raw)+2.5*sd(rt_raw))) %>% 
  group_by(Condition, Subject, match, StudyNumber) %>% 
  summarise(Value = mean(rt_raw)) %>% 
  mutate(RTorError = "RT")

#calculate er
Pavlidou_2018_er <- Pavlidou_2018 %>% 
  group_by(Condition, Subject, match, StudyNumber) %>% 
  summarise(Value = mean(corr_raw)) %>% 
  mutate(
    RTorError = "Error",
    Value = 1- Value) #change acc to err

#join er and rt
Pavlidou_2018 <- rbind(Pavlidou_2018_er, Pavlidou_2018_rt)

#add and rename some columns to fit format
Pavlidou_2018 <- Pavlidou_2018 %>% 
  mutate(
    Paper = "Pavlidou_2018",
    Age = NA,
    Sex = NA,
    Consistency = case_when(grepl("_consistent_", Condition) ~ "con",
                            grepl("_inconsistent_", Condition) ~ "incon"),
    Matching = case_when(
      match=="matching"~"match",
      match=="mismatching"~"mismatch"),
    SelfOther = "self") 

Pavlidou_2018 <- subset(Pavlidou_2018, select = -c(match)) #remove redundant column

#merge with coding df
Pavlidou_2018_Codes <- filter(classifications, Paper == "Pavlidou_2018") 


Pavlidou_2018 <- inner_join(Pavlidou_2018_Codes, Pavlidou_2018)
```

##Pavlidou 2019
```{r}
Pavlidou_2019 <- read_csv("../data_files/Pavlidou_2019.csv")

#remove filler trials
Pavlidou_2019 <- Pavlidou_2019 %>%
  filter(fillers == 'no') %>% 
  select(-fillers)

#consolidate columns
Pavlidou_2019 <- Pavlidou_2019 %>% 
  mutate(
    Condition = paste("self", consistency, task, substr(condition,1,8), sep = "_"),
    Subject = paste("Pavlidou_2018", subject, case_when(task =="avatar" ~ "Ex1", task == "arrow" ~ "Ex2"), sep = "_"),
    StudyNumber = case_when(task =="avatar" ~ 1, task == "arrow" ~ 2)
  ) %>% 
  select(-c(consistency, task, condition, subject, task))

#calculate mean rt
Pavlidou_2019_rt <- Pavlidou_2019 %>% 
  filter(corr_raw == 1) %>%  #remove incorrect trials
  group_by(Subject) %>% 
  filter(rt_raw >= (mean(rt_raw)-2.5*sd(rt_raw)) & rt_raw <= (mean(rt_raw)+2.5*sd(rt_raw))) %>% #filter to 2.5 sd from mean for each participant
  group_by(Condition, Subject, match, StudyNumber) %>% 
  summarise(Value = mean(rt_raw)) %>% #mean at subject x condition level
  mutate(RTorError = "RT")

#calculate mean er
Pavlidou_2019_er <- Pavlidou_2019 %>% 
  group_by(Condition, Subject, match, StudyNumber) %>% 
  summarise(Value = mean(corr_raw)) %>% 
  mutate(RTorError = "Error")

#join er and rt
Pavlidou_2019 <- rbind(Pavlidou_2019_er, Pavlidou_2019_rt)

#add and rename some columns to fit format
Pavlidou_2019 <- Pavlidou_2019 %>% 
  mutate(
    Paper = "Pavlidou_2019",
    Age = NA,
    Sex = NA,
    match = case_when(match == "matching" ~ "match", match == "mismatching" ~ "mismatch"),
    Consistency = case_when(grepl("_consistent_", Condition) ~ "con",
                            grepl("_inconsistent_", Condition) ~ "incon"),
    SelfOther = "self",
    Value = case_when(
      RTorError == "RT" ~ Value*1000, #change sec to ms
      RTorError == "Error" ~ 1-Value) #change acc to err
      ) %>% 
  ungroup() %>% 
  as.data.frame() %>% 
  rename(Matching = match)

#merge with coding df
Pavlidou_2019_Codes <- filter(classifications, Paper == "Pavlidou_2019") 
Pavlidou_2019 <- left_join(Pavlidou_2019_Codes, Pavlidou_2019)
```

##Cole 2016
```{r}
#load data
Cole_2016 <- read.csv("../data_files/Cole_2016.csv")


Cole_2016 <- Cole_2016 %>% 
  rename(Subject = X) %>% 
  filter(X..errors <= 20, Subject != 1) %>% #exclude >20% errors and subject 1 (flawed)
  select(-X..errors, -errors)

#remove mismatch columns because consistency cannot be determined within these trials, given this dataset
Cole_2016 <- Cole_2016 %>% select(-Vis_Mismatch_RT, -NonVis_Mismatch_RT, -Vis_Mismatch_ER, -NonVis_Mismatch_ER)

#wide to long
Cole_2016 <- Cole_2016 %>% 
  gather(key = Condition, value = Value, Vis_Match_Con_RT:NonVis_Match_Incon_ER)

#add columns
Cole_2016 <- Cole_2016 %>%
  mutate(
    RTorError = case_when(substr(Condition, nchar(Condition), nchar(Condition)) == "R" ~ "Error", substr(Condition, nchar(Condition), nchar(Condition)) == "T" ~ "RT"),
    Value = case_when(RTorError == "Error" ~ as.numeric(Value)/36, RTorError == "RT" ~ as.numeric(Value)), #convert num errors to ER (36 trials per condition)
    Value = as.single(Value),
    Sex = NA,
    Age = NA,
    StudyNumber = 1,
    Paper = "Cole_2016",
    Matching = "match",
    Subject = paste("Cole_2016", Subject, sep = "_"),
    Consistency = case_when(grepl("_Con_", Condition) ~ "con",
                            grepl("_Incon_", Condition) ~ "incon"),
    SelfOther = "self")

#merge with coding df
Cole_2016_Codes <- filter(classifications, Paper == "Cole_2016") 

Cole_2016 <- inner_join(Cole_2016_Codes, Cole_2016)
```

##Deliens 2018
```{r}
Deliens_2018 <- read.csv(("../data_files/Deliens_2018.csv"))

#eliminate outliers
Deliens_2018 <- Deliens_2018 %>% 
  filter(outliers != "outlier")  %>% 
  select(-outliers)

#wide to long
Deliens_2018 <- Deliens_2018 %>% gather(key = Condition, value = Value, SD_RT_OtherCon:Sleep_IES_IncALL)

#remove summary stats
Deliens_2018 <- Deliens_2018 %>%
  filter(
    !grepl("ALL", Condition), 
    !grepl("Total", Condition),
    !grepl("IES", Condition)
    )

#add in necessary columns
Deliens_2018 <- Deliens_2018 %>%
  mutate(
    RTorError = case_when(grepl("RT", Condition) ~ "RT", grepl("ACC", Condition) ~ "Error"),
    Sex = NA,
    Age = NA,
    Matching = "match",
    Paper = "Deliens_2018",
    StudyNumber = 1,
    Subject = paste("Deliens_2018", Subject, sep = "_"),
    SelfOther = case_when(
      grepl("Other", Condition) ~ "other",
      grepl("Self", Condition) ~ "self"),
    Consistency = case_when(
      grepl("Inc", Condition) ~ "con",
      grepl("Con", Condition) ~ "incon"),
    Value = case_when(RTorError=="Error"~ 1 - Value, TRUE ~ Value) #change acc to err
  )

#merge with coding df
Deliens_2018_Codes <- filter(classifications, Paper == "Deliens_2018") 
Deliens_2018 <- inner_join(Deliens_2018_Codes, Deliens_2018)
```

##Deroualle 2017
```{r}
Deroualle_2017 <- read.csv("../data_files/Deroualle_2017.csv")

#wide to long
Deroualle_2017 <- Deroualle_2017 %>% 
  gather(key = Consistency,
         value = Value,
         c(Con, Incon))

#create new columns
Deroualle_2017 <- Deroualle_2017 %>% 
  mutate(
    Condition = paste(Consistency, Condition, SelfOther, Agent, sep = "_"),
    Sex = NA,
    Age = NA,
    Matching = "match",
    Subject = paste("Deroualle_2017", Subject, sep = "_"),
    Paper = "Deroualle_2017",
    StudyNumber = 1,
    RTorError = "RT",
    Consistency = case_when(
      Consistency=="Con" ~ "con",
      Consistency=="Incon" ~ "incon"
    )
  ) %>% 
  select(-Agent)

#merge with coding df
Deroualle_2017_Codes <- filter(classifications, Paper == "Deroualle_2017") 
Deroualle_2017 <- inner_join(Deroualle_2017_Codes, Deroualle_2017)
```

##Todd 2019 (experiments 1 and 2 only)
```{r}
Todd_2019_ex1 <- read_sav("../data_files/Todd_2019_1.sav")
Todd_2019_ex2 <- read_sav("../data_files/Todd_2019_2.sav")

#Experiment 1

#remove special character from colname
names(Todd_2019_ex1)[names(Todd_2019_ex1)  == "filter_$"] <- "filter"

#select relevant columns and remove level 2 vpt results (different task) and excluded subjects (filter == 0)
Todd_2019_ex1 <- Todd_2019_ex1 %>% 
  filter(Level == 1, filter == 1) %>%
  select(LOC_RT, LOI_RT, LSC_RT, LSI_RT, SOC_RT, SOI_RT, SSC_RT, SSI_RT, LOC_error, LOI_error, LSC_error, LSI_error, SOC_error, SOI_error, SSC_error, SSI_error, ID, subsex)

#wide to long
Todd_2019_ex1 <- Todd_2019_ex1 %>% 
  gather(key = Condition, value = Value, LOC_RT:SSI_error)

#change colnames and nomenclature
Todd_2019_ex1 <- Todd_2019_ex1 %>% 
  mutate(
    Sex = case_when(subsex == 1 ~ "M", subsex == 2 ~ "F"),
    RTorError = case_when(grepl("error", Condition) ~ "Error", grepl("RT", Condition) ~ "RT"),
    Age = NA,
    Paper = "Todd_2019",
    StudyNumber = 1,
    Condition = paste(Condition, StudyNumber, sep = "_"),
    Subject = paste("Todd_2019", ID, "Ex1", sep = "_"),
    Matching = "match") %>% 
  select(-subsex, -ID) #remove outdated columns


#Experiment 2

#remove special character from colname
names(Todd_2019_ex2)[names(Todd_2019_ex2)  == "filter_$"] <- "filter"

#select relevant columns and remove level 2 vtp, exclude filter = 0 subjects
Todd_2019_ex2 <- Todd_2019_ex2 %>% 
  filter(Level == 1, filter == 1) %>% 
  select(ID, subsex, Deadline, OC_error, OI_error, SC_error, SI_error, OC_RT, OI_RT, SC_RT, SI_RT)

#wide to long
Todd_2019_ex2 <- Todd_2019_ex2 %>% 
  gather(key = Condition, value = Value, OC_error:SI_RT)
  
#change colnames and nomenclature
Todd_2019_ex2 <- Todd_2019_ex2 %>% 
  mutate(
    StudyNumber = 2,
    Sex = case_when(subsex == 1 ~ "M", subsex == 2 ~ "F"),
    RTorError = case_when(grepl("error", Condition) ~ "Error", grepl("RT", Condition) ~ "RT"),
    Condition = paste(case_when(Deadline == 2 ~ "L", Deadline == 1 ~ "S"), Condition, sep = ""), #2=Long 1=short deadline
    Condition = paste(Condition, StudyNumber, sep = "_"),
    Age = NA,
    Paper = "Todd_2019",
    Subject = paste("Todd_2019", ID, "Ex2", sep = "_"),
    Matching = "match") %>% 
  select(-subsex, -ID, -Deadline) #remove outdated columns

#join ex 1 and 2
Todd_2019 <- rbind(Todd_2019_ex1, Todd_2019_ex2)

Todd_2019 <- Todd_2019 %>% 
  mutate(
    SelfOther = case_when(
      substr(Condition, 2, 2)=="O" ~ "other",
      substr(Condition, 2, 2)=="S" ~ "self"),
    Consistency = case_when(
      substr(Condition, 3, 3)=="C" ~ "con",
      substr(Condition, 3, 3)=="I" ~ "incon")
  )

#merge with classification df
Todd_2019_Codes <- filter(classifications, Paper == "Todd_2019") 

Todd_2019 <- left_join(Todd_2019, Todd_2019_Codes)
```


##Todd 2020 (expts 1,2,3,5)
```{r}
Todd_2020_ex1 <- read_sav("../data_files/Todd_2020_1.sav")
Todd_2020_ex2 <- read_sav("../data_files/Todd_2020_2.sav")
Todd_2020_ex3 <- read_sav("../data_files/Todd_2020_3.sav")
Todd_2020_ex4 <- read_sav("../data_files/Todd_2020_4.sav")
Todd_2020_ex5 <- read_sav("../data_files/Todd_2020_5.sav")

#Experiment 1

#remove special char from colname
names(Todd_2020_ex1)[names(Todd_2020_ex1)  == "filter_$"] <- "filter"

#select relevant rows
Todd_2020_ex1 <- Todd_2020_ex1 %>% 
  filter(level == 1, filter == 1) %>% 
  select(ID:block_order, OC_RT:SI_error, -subrace, -level)

#wide to long
Todd_2020_ex1 <- Todd_2020_ex1 %>% 
  gather(key = Condition, value = Value, OC_RT:SI_error)

#change colnames and coding scheme
Todd_2020_ex1 <- Todd_2020_ex1 %>% 
  mutate(
    RTorError = case_when(grepl("RT", Condition) ~ "RT", grepl("error", Condition) ~ "Error"),
    Subject = paste("Todd_2020_ex1", ID, sep = "_"),
    Condition = paste(Condition, case_when(block_order == 1 ~"AvFirst", block_order == 2 ~ "SelfFirst", TRUE ~ "mixed"), sep = "_"),
    Sex = case_when(subgen == 1 ~ "M", subgen == 2 ~ "F"),
    Matching = "match",
    Paper = "Todd_2020",
    StudyNumber = 1, 
    Age = NA
  ) %>% 
  select(-subgen, -blocking, -block_order, -ID)


#Experiment 2
#remove special char from colname
names(Todd_2020_ex2)[names(Todd_2020_ex2)  == "filter_$"] <- "filter"

#select relevant rows
Todd_2020_ex2 <- Todd_2020_ex2 %>% 
  filter(level == 1, filter == 1) %>% 
  select(ID:block_order, OC_RT:SI_error, -subrace, -level)

#wide to long
Todd_2020_ex2 <- Todd_2020_ex2 %>% 
  gather(key = Condition, value = Value, OC_RT:SI_error)

#change colnames and coding scheme
Todd_2020_ex2 <- Todd_2020_ex2 %>% 
  mutate(
    Condition = paste(Condition, case_when(block_order==1~"AvFirst", block_order==2~"SelfFirst"), sep = "_"),
    RTorError = case_when(grepl("RT", Condition) ~ "RT", grepl("error", Condition) ~ "Error"),
    Subject = paste("Todd_2020_ex2", ID, sep = "_"),
    Sex = case_when(subgen == 1 ~ "M", subgen == 2 ~ "F"),
    Matching = "match",
    Paper = "Todd_2020",
    StudyNumber = 2, 
    Age = NA
  ) %>% 
  select(-subgen, -ID, -block_order)


#Experiment 3
#remove special char from colname
names(Todd_2020_ex3)[names(Todd_2020_ex3)  == "filter_$"] <- "filter"

#select relevant rows
Todd_2020_ex3 <- Todd_2020_ex3 %>% 
  filter(filter == 1, level == 1) %>% 
  select(ID:block_order, OC_RT:SI_error, -subrace, -level)

#wide to long
Todd_2020_ex3 <- Todd_2020_ex3 %>% 
  gather(key = Condition, value = Value, OC_RT:SI_error)

#change colnames and coding scheme
Todd_2020_ex3 <- Todd_2020_ex3 %>% 
  mutate(
    RTorError = case_when(grepl("RT", Condition) ~ "RT", grepl("error", Condition) ~ "Error"),
    Condition = paste(Condition, case_when(block_order==1~"AvFirst", block_order==2~"SelfFirst"), sep = "_"),
    Subject = paste("Todd_2020_ex3", ID, sep = "_"),
    Sex = case_when(subgen == 1 ~ "M", subgen == 2 ~ "F"),
    Matching = "match",
    Paper = "Todd_2020",
    StudyNumber = 3, 
    Age = NA
  ) %>% 
  select(-subgen, -ID, -block_order)


#Experiment 5
#remove special char from colname
names(Todd_2020_ex5)[names(Todd_2020_ex5)  == "filter_$"] <- "filter"

#select relevant rows
Todd_2020_ex5 <- Todd_2020_ex5 %>% 
  filter(filter == 1) %>% 
  select(ID, subgen, first_block, OC_RT:SI_RT, OC_error:SI_error)

#wide to long
Todd_2020_ex5 <- Todd_2020_ex5 %>% 
  gather(key = Condition, value = Value, OC_RT:SI_error) %>% 
  filter(!is.na(Value)) #remove na values

#change colnames and coding scheme
Todd_2020_ex5 <- Todd_2020_ex5 %>%
  mutate(
    RTorError = case_when(grepl("RT", Condition) ~ "RT", grepl("error", Condition) ~ "Error"),
    Condition = paste(Condition, case_when(first_block==1 ~ "AvFirst", first_block==2 ~ "ControlTask"), sep = "_"),
    Subject = paste("Todd_2020_ex5", ID, sep = "_"),
    Age = NA,
    Paper = "Todd_2020",
    StudyNumber = 5,
    Sex = case_when(subgen == 1 ~ "M", subgen == 2 ~ "F"),
    Matching = "match"
  ) %>% 
  select(-ID, -subgen, -first_block)

Todd_2020 <- rbind(Todd_2020_ex1, Todd_2020_ex2, Todd_2020_ex3, Todd_2020_ex5)

Todd_2020 <- Todd_2020 %>% 
  mutate(
    SelfOther = case_when(
      substr(Condition,1,1) == "O" ~ "other",
      substr(Condition,1,1) == "S" ~ "self"),
    Consistency = case_when(
      substr(Condition, 2,2) == "C" ~ "con",
      substr(Condition, 2,2) == "I" ~ "incon")
  )

#merge with classification df
Todd_2020_Codes <- filter(classifications, Paper == "Todd_2020") 

Todd_2020 <- left_join(Todd_2020, Todd_2020_Codes)
```

##McCleery 2011
```{r}
McCleery_2011 <- read_sav("../data_files/McCleery_2011.sav")

#select relevant cols
McCleery_2011 <- McCleery_2011 %>% 
  select(subject:selfinconrt) %>% 
  rename(otherconrt = ohterconrt) #fix spelling error

#wide to long
McCleery_2011 <- McCleery_2011 %>% 
  gather(key = Condition, value = Value, otherconacc:selfinconrt)

#change colnames and coding scheme
McCleery_2011 <- McCleery_2011 %>% 
  mutate(
    RTorError = case_when(grepl("acc", Condition) ~ "Error", grepl("rt", Condition) ~ "RT"),
    Paper = "McCleery_2011",
    Subject = paste("McCleery_2011", subject, sep = "_"),
    Sex = NA,
    Age = NA,
    StudyNumber = 1, 
    Matching = "mixed",
    SelfOther = case_when(
      substr(Condition, 1, 5)=="other" ~ "other",
      substr(Condition, 1, 4)=="self" ~ "self"),
    Consistency = case_when(
      grepl("fcon", Condition) ~ "con",
      grepl("rcon", Condition) ~ "con",
      grepl("fincon", Condition) ~ "incon",
      grepl("rincon", Condition) ~ "incon"),
     Value = case_when( #change accuracy to error
       RTorError == "Error" ~ 1 - Value,
       TRUE ~ Value)
  ) %>% 
  select(-subject)

#merge with classification df
McCleery_2011_Codes <- filter(classifications, Paper == "McCleery_2011") 

McCleery_2011 <- left_join(McCleery_2011, McCleery_2011_Codes)
```

##Surtees 2012
```{r}
Surtees_2012 <- read.csv("../data_files/Surtees_2012.csv")
#this data only has 10 of 11 adult subjects (one missing)

#add subject column
Surtees_2012 <- Surtees_2012 %>% 
  rowid_to_column("Subject")

#wide to long
Surtees_2012 <- Surtees_2012 %>% 
  gather(key = Condition, value = Value, OtherConsistentRT:SelfInconsistentER)

#rework colnames and coding
Surtees_2012 <- Surtees_2012 %>% 
  mutate(
    Subject = paste("Surtees_2012", Subject, sep = "_"),
    Paper = "Surtees_2012",
    StudyNumber = case_when(Age == "Stick" ~ 2, TRUE ~ 1),
    Age = case_when(Age == "Stick" ~ 8, Age == "Adult" ~ as.numeric(NA), TRUE ~ as.numeric(as.character(Age))),
    Sex = NA,
    RTorError = case_when(grepl("ER",Condition) ~ "Error", grepl("RT", Condition) ~ "RT"),
    Matching = "match",
    SelfOther = case_when(
      grepl("Self", Condition) ~ "self",
      grepl("Other", Condition) ~ "other"),
    Consistency = case_when(
      grepl("Con", Condition) ~ "con",
      grepl("Incon", Condition) ~ "incon"),
    Condition = paste(Condition, StudyNumber, sep = "_")
  )

#merge with classification df
Surtees_2012_Codes <- filter(classifications, Paper == "Surtees_2012") 

Surtees_2012 <- left_join(Surtees_2012, Surtees_2012_Codes)
```

##Surtees 2016
```{r}
Surtees_2016 <- read_sav("../data_files/Surtees_2016.sav")

#select relevant data
Surtees_2016 <- Surtees_2016 %>% 
  filter(Level == 1) %>% 
  select(-OCPC:-SIPC, -Level) %>% 
  rename(OCacc = OtherconsistentAcc, OIacc = OIAcc, SIacc = SIAcc) #fix random naming inconsistencies

#wide to long
Surtees_2016 <- Surtees_2016 %>% 
  gather(key = Condition, value = Value, OCacc:SIRT)

#change colnames and coding
Surtees_2016 <- Surtees_2016 %>% 
  mutate(
    RTorError = case_when(
      substr(Condition, nchar(Condition)-1, nchar(Condition)) == "RT" ~ "RT", 
      substr(Condition, nchar(Condition)-2, nchar(Condition)) == "acc" ~ "Error"),
    SelfOther = case_when(
      substr(Condition, 1, 1) == "S" ~ "self",
      substr(Condition, 1, 1) == "O" ~ "other"),
    Consistency = case_when(
      substr(Condition, 2, 2) == "C" ~ "con",
      substr(Condition, 2, 2) == "I" ~ "incon"),
    Condition = paste(
      Condition,
      case_when( #double checked from mean RT blocked < mixed
        FirstBlock == 1 ~ "mixed", 
        FirstBlock == 2 ~ "SelfFirst",
        FirstBlock == 3 ~ "OtherFirst"), 
      sep = "_"),
    Paper = "Surtees_2016",
    StudyNumber = 1,
    Subject = paste("Surtees_2016", participant, sep = "_"),
    Matching = "match",
    Value = case_when(RTorError == "Error" ~ 1-Value, RTorError == "RT" ~ Value), #change accuracy to error rate
    Age = NA,
    Sex = NA
  ) %>% 
  select(-participant, -BlockMix, -FirstBlock)

#merge with classification df
Surtees_2016_Codes <- filter(classifications, Paper == "Surtees_2016") 

Surtees_2016 <- left_join(Surtees_2016, Surtees_2016_Codes)
```

##Doi 2020
```{r}
Doi_2020_Acc <- read.csv("../data_files/Doi_2020_Acc.csv")
Doi_2020_RT <- read.csv("../data_files/Doi_2020_RT.csv")

#Remove summary stats
Doi_2020_Acc <- Doi_2020_Acc %>% 
  filter(X != 'AVG', X != "SD")
Doi_2020_RT <- Doi_2020_RT %>% 
  filter(X != 'AVG', X != "SD")

#average incon1 and incon2
Doi_2020_Acc <- Doi_2020_Acc %>% 
  mutate(
    Self_Switch_Incon = (Self_Switch_Inconsistent1+Self_Switch_Inconsistent2)/2,
    Other_Switch_Incon = (Other_Switch_Inconsistent1+Other_Switch_Inconsistent2)/2,
    Self_Nonswitch_Incon = (Self_Nonswitch_Inconsistent1+Self_Nonswitch_Inconsistent2)/2,
    Other_Nonswitch_Incon = (Other_Nonswitch_Inconsistent1+Other_Nonswitch_Inconsistent2)/2,
    .keep = "unused")
Doi_2020_RT <- Doi_2020_RT %>% 
  mutate(
    Self_Switch_Incon = (Self_Switch_Inconsistent1+Self_Switch_Inconsistent2)/2,
    Other_Switch_Incon = (Other_Switch_Inconsistent1+Other_Switch_Inconsistent2)/2,
    Self_Nonswitch_Incon = (Self_Nonswitch_Inconsistent1+Self_Nonswitch_Inconsistent2)/2,
    Other_Nonswitch_Incon = (Other_Nonswitch_Inconsistent1+Other_Nonswitch_Inconsistent2)/2,
    .keep = "unused")


#average switch and nonswitch
Doi_2020_Acc <- Doi_2020_Acc %>%
  mutate(
    Self_Consistent = (Self_Switch_Consistent+Self_Nonswitch_Consistent)/2,
    Other_Consistent = (Other_Switch_Consistent+Other_Nonswitch_Consistent)/2,
    Self_Incon = (Self_Switch_Incon+Self_Nonswitch_Incon)/2,
    Other_Incon = (Other_Switch_Incon+Other_Nonswitch_Incon)/2,
    .keep = "unused")
Doi_2020_RT <- Doi_2020_RT %>%
  mutate(
    Self_Consistent = (Self_Switch_Consistent+Self_Nonswitch_Consistent)/2,
    Other_Consistent = (Other_Switch_Consistent+Other_Nonswitch_Consistent)/2,
    Self_Incon = (Self_Switch_Incon+Self_Nonswitch_Incon)/2,
    Other_Incon = (Other_Switch_Incon+Other_Nonswitch_Incon)/2,
    .keep = "unused"
  )

#wide to long
Doi_2020_Acc <- Doi_2020_Acc %>%
  gather(key = Condition, value = Value, Self_Consistent:Other_Incon
 ) %>% 
  mutate(
    RTorError = "Error",#mark as error
    Value = 1-(Value/100) #switch acc to ER
    ) 

Doi_2020_RT <- Doi_2020_RT %>%
  gather(key = Condition, value = Value, Self_Consistent:Other_Incon
 ) %>% 
  mutate(
    RTorError = "RT", #mark as RT
    Value = Value*1000 #switch sec to ms
    )

#join RT and Error
Doi_2020 <- rbind(Doi_2020_Acc, Doi_2020_RT)

#change colnames and coding scheme
Doi_2020 <- Doi_2020 %>% 
  mutate(
    Subject = paste("Doi_2020", X, sep = "_"),
    Sex = NA,
    Age = NA,
    Paper = "Doi_2020",
    StudyNumber = 1,
    Matching = "match",
    SelfOther = case_when(grepl("Self", Condition)~"self", TRUE ~ "other"),
    Consistency = case_when(grepl("Consistent", Condition)~"con", TRUE ~ "incon")
  ) %>% 
  select(-X)

#merge with classification df
Doi_2020_Codes <- filter(classifications, Paper == "Doi_2020") 

Doi_2020 <- left_join(Doi_2020, Doi_2020_Codes)
```

##Marshall 2018
```{r}
Marshall_2018_1 <- read_sav("../data_files/Marshall_2018_1.sav")
Marshall_2018_2 <- read_sav("../data_files/Marshall_2018_2.sav")

#Experiment 1
#select correct level of summary stat (participant x animacy x consistency x perspective)
Marshall_2018_1 <- Marshall_2018_1 %>% 
  select(RT_SCH:RT_OIA,Gender,Age) %>% 
  rowid_to_column("Subject")

#wide to long
Marshall_2018_1 <- Marshall_2018_1 %>% 
  gather( key = Condition, value = Value, RT_SCH:RT_OIA) %>% 
  filter(!is.na(Value))

#change colnames and coding scheme
Marshall_2018_1 <- Marshall_2018_1 %>% 
  mutate(
    RTorError = "RT",
    Paper = "Marshall_2018",
    StudyNumber = 1,
    Matching = NA, #no number cue was presented in this task
    Subject = paste("Marshall_2018_1", Subject, sep = "_"),
    Sex = case_when(Gender == 1 ~ "M", Gender == 2 ~ "F"),
    Consistency = case_when(
      substr(Condition, 5, 5)=="C" ~ "con", 
      substr(Condition, 5, 5)=="I" ~ "incon"),
    SelfOther = case_when(
      substr(Condition, 4, 4)=="S" ~ "self",
      substr(Condition, 4, 4)=="O" ~ "other")
  ) %>% 
  select(-Gender)


#Experiment 2
#add subject
Marshall_2018_2 <- Marshall_2018_2 %>% 
  rowid_to_column("Subject")

#wide to long
Marshall_2018_2 <- Marshall_2018_2 %>% 
  gather(key = Condition, value = Value, c(YouConsEyes_RT:HimInconsisNosee_RT, YouConsisSee_errors:HimInconsisEyes_errors)) %>%
  filter(!is.na(Value))

#select cols
Marshall_2018_2 <- Marshall_2018_2 %>% 
  select(-condition,-politicalo:-religion)


#change colnames and coding scheme
Marshall_2018_2 <- Marshall_2018_2 %>% 
  mutate(
    RTorError = case_when(grepl("RT", Condition)~"RT", grepl("errors", Condition)~"Error"),
    Paper = "Marshall_2018",
    StudyNumber = 2, 
    Value = case_when(RTorError == "Error" ~ (Value/24), TRUE ~ Value), #24 trials per condition combo (288 trials total and (2x2x3) design)
    Matching = "match",
    Subject = paste("Marshall_2018_2", Subject, sep = "_"),
    Sex = case_when(gender == 1 ~ "M", gender == 2 ~ "F"),
    Consistency = case_when(
      grepl("Cons", Condition) ~ "con",
      grepl("Incons", Condition) ~ "incon"),
    SelfOther = case_when(
      grepl("Him", Condition) ~ "other",
      grepl("You", Condition) ~ "self")
  ) %>% 
  rename(Age = age) %>% 
  select(-gender) 


Marshall_2018 <- rbind(Marshall_2018_1, Marshall_2018_2)

#merge with classification df
Marshall_2018_Codes <- filter(classifications, Paper == "Marshall_2018") 
Marshall_2018 <- left_join(Marshall_2018, Marshall_2018_Codes)
```

##Langton 2018
```{r}
Langton_2018_1 <- read.csv("../data_files/Langton_2018_1.csv")
Langton_2018_2 <- read.csv("../data_files/Langton_2018_2.csv")

#Expt 1
#create conditon column
Langton_2018_1 <- Langton_2018_1 %>% 
  mutate(
    Condition = paste(Consistency, Barrier, sep = "_"),
  ) %>% 
  ungroup() %>% 
  select(-c(Consistency, Barrier)) %>% 
  rename(Value = StimulusDisplay.RT)

#average at condition x subject level
Langton_2018_1 <- Langton_2018_1 %>% 
  group_by(Subject) %>% 
  filter(Value >= (mean(Value)-2.5*sd(Value)) & Value <= (mean(Value)+2.5*sd(Value))) %>% #filter to 2.5 sd from mean for each subject
  group_by(Subject, Condition) %>% 
  summarise(Value = mean(Value))

#change colnames and coding scheme
Langton_2018_1 <- Langton_2018_1 %>% 
  mutate(
    Sex = NA,
    Age = NA,
    Matching = "match",
    Paper = "Langton_2018",
    StudyNumber = 1,
    RTorError = "RT",
    Subject = paste("Langton_2018_1", Subject, sep = "_")
  )
  
  
#Expt 2
#create conditon column
Langton_2018_2 <- Langton_2018_2 %>% 
  mutate(
    Condition = paste(Consistency, Barrier, sep = "_")
  ) %>% 
  ungroup() %>% 
  select(-c(Consistency, Barrier)) %>% 
  rename(Value = ImageDisplay2.RT.Trial.)

#average at condition x subject level
Langton_2018_2 <- Langton_2018_2 %>% 
  group_by(Subject) %>% 
  filter(Value >= (mean(Value)-2.5*sd(Value)) & Value <= (mean(Value)+2.5*sd(Value))) %>% #filter to 2.5 sd from the mean for each sub
  group_by(Subject, Condition) %>% 
  summarise(Value = mean(Value))

#change colnames and coding scheme
Langton_2018_2 <- Langton_2018_2 %>% 
  mutate(
    Sex = NA,
    Age = NA,
    Matching = "match",
    Paper = "Langton_2018",
    StudyNumber = 2,
    RTorError = "RT",
    Subject = paste("Langton_2018_2", Subject, sep = "_")
  )

Langton_2018 <- rbind(Langton_2018_1, Langton_2018_2)

Langton_2018 <- Langton_2018 %>% 
  mutate(
    Condition = paste(Condition, StudyNumber, sep = "_"),
    Consistency = case_when(grepl("Con", Condition) ~ "con", grepl("Incon", Condition) ~ "incon"),
    SelfOther = "self")

#merge with classification df
Langton_2018_Codes <- filter(classifications, Paper == "Langton_2018") 
Langton_2018 <- left_join(Langton_2018, Langton_2018_Codes)
```

##Garnder and Beliveciute 2018 (ex2 only--DPT)
```{r}
Gardner_B_2018_2 <- read_sav("../data_files/Gardner_B_ex2.sav")

#select relevant columns
Gardner_B_2018_2 <- Gardner_B_2018_2 %>% 
  select(-Handedness, -PE.total, -resp_omit_time)

#apply exclusions
Gardner_B_2018_2 <- Gardner_B_2018_2 %>% 
  filter(include == 1) %>% 
  select(-include)

#wide to long
Gardner_B_2018_2 <- Gardner_B_2018_2 %>% 
  gather(key = Condition, value = Value, RT.Av.no:PE.Ma.yes)

#change colnames and coding nomenclature
Gardner_B_2018_2 <- Gardner_B_2018_2 %>% 
  mutate(
    RTorError = case_when(grepl("RT", Condition) ~ "RT", grepl("PE", Condition) ~ "Error"),
    Consistency = case_when(grepl("no", Condition) ~ "incon", grepl("yes", Condition) ~ "con"),#this is verified by reproduced anova below
    Sex = case_when(Sex == "female" ~ "F", Sex == "male" ~ "M"),
    Paper = "Gardner_Beliveciute_2018",
    Subject = paste("Gardner_B_2018", subject, sep = "_"),
    StudyNumber = 2, 
    SelfOther = "self",
    Value = case_when(RTorError=="Error" ~ Value/100, TRUE ~ Value),
    Matching = "match"
  ) %>% 
  select(-subject)

##Reproduce anova (double check that yes = consistent and no = inconsistent)
#Gardner_B_2018_2 <- Gardner_B_2018_2 %>% 
#  mutate(
#    Gaze = case_when(grepl("Av", Condition) ~ "avert", grepl("Ma", Condition) ~ "Maintain")
#  )
#  
#Gardner_rt <- filter(Gardner_B_2018_2, RTorError == "RT")
#Gardner_av_rep <- Gardner_rt  %>% 
#  anova_test(dv = Value, wid = subject, within = c(Consistent, Gaze))

 
#merge with classification df
Gardner_B_2018_2_Codes <- filter(classifications, Paper == "Gardner_B_2018") 
Gardner_B_2018_2  <- left_join(Gardner_B_2018_2 , Gardner_B_2018_2_Codes, by = "Condition")


Gardner_B_2018_2 <- Gardner_B_2018_2 %>% 
  mutate(Paper = Paper.x) %>% 
  select(-Paper.y, -Paper.x)
```

##Ogrady 2017
```{r}
OGrady_2017 <- read_delim("../data_files/OGrady_2017.csv", delim = "\t", col_names = c("computerNumber", "participantNumber", "condition", "yesSide", "participantGender", "trialNumber", "blockNumber", "flipped", "BA","image","ballsInScene","avGender","genderMatch","consistency","numberShown","perspective","match","responseKey","response","RT","correctResponse", "correct"))

OGrady_2017 <- OGrady_2017 %>% 
  mutate(
    BA = substr(image, 1, 1),
    A = substr(image, 2, 2),
    S = substr(image, 6, 6),
    BS = substr(image, 7, 7),
    Behind = case_when((BA!=0 & A==1)|(BS!=0 & S==1) ~ 'behind', TRUE ~ 'front')
  )

OGrady_2017 <- OGrady_2017 %>% 
  select(participantNumber, condition, participantGender, blockNumber, genderMatch, consistency, match, correct, RT, Behind) %>% 
  filter(blockNumber != "test") %>% #remove practice
  mutate(match= case_when(match=="yes"~"match", TRUE~"mismatch"))

#calculate subject x conditiion average er
OGrady_2017_er <- OGrady_2017 %>%
  group_by(participantNumber, condition, participantGender, genderMatch, consistency, match, Behind) %>% 
  summarise(Value = mean(correct)) %>% 
  mutate(RTorError = "Error")

#calculate subject x condition average rt
OGrady_2017_rt <- OGrady_2017 %>%
  filter(correct==1) %>% #remove incorrect
  group_by(participantNumber) %>% 
  filter(RT >= (mean(RT)-2.5*sd(RT)) & RT <= (mean(RT)+2.5*sd(RT))) %>% #filter to 2.5 SD from mean
  group_by(participantNumber, condition, participantGender, genderMatch, consistency, match, Behind) %>% 
  summarize(Value = mean(RT)) %>% 
  mutate(RTorError = "RT")

OGrady_2017 <- rbind(OGrady_2017_er, OGrady_2017_rt)

#reorganize columns
OGrady_2017 <- OGrady_2017 %>% 
  mutate(
    Subject = paste("Ogrady_2017", participantNumber, sep ="_"),
    Paper = "OGrady_2017",
    StudyNumber = 1,
    Sex = case_when(participantGender=='m' ~ 'M', participantGender=="f" ~ "F"),
    Age =NA,
    Consistency = case_when(consistency=="consistent"~"con", consistency=="inconsistent"~"incon"),
    SelfOther = 'self',
    Condition = paste(
      condition, 
      case_when(
        Consistency=="con" ~ Consistency,
        Consistency=="incon"  ~ paste(Consistency, Behind, sep = "_")),
      case_when(genderMatch==0~"GenNoMatch", genderMatch==1~"GenMatch"), 
      Behind, 
      sep = "_")
  ) %>% 
  ungroup() %>% 
  select(-c(participantNumber, participantGender, consistency, Behind, genderMatch, condition)) %>% 
  rename(Matching = match) %>% 
  mutate(Value = case_when(RTorError == "Error" ~ 1-Value, TRUE~Value))

#merge with classification df
OGrady_2017_Codes <- filter(classifications, Paper == "OGrady_2017") 

OGrady_2017  <- left_join(OGrady_2017, OGrady_2017_Codes)
```

##Wilson 2017
- EX1 EXCLUDED
- Ex1 had error values >1. Given that other error measures recorded in this analysis do not permit error>1, ex1 will be excluded, as the meaning of these values is unknown.
- Ex2 (rt and error) and Ex4 (error only) have been verified by reproduced anovas. These data will be included. 
```{r}
wilson_2017_er_1 <- read_sav("../data_files/Wilson2017_Exp1_Errors(Replica).sav")
wilson_2017_rt_1 <- read_sav("../data_files/Wilson2017_EXP1_RT(replica).sav")
wilson_2017_1_blind <- read.csv("../data_files/Wilson_2017_ex1_Blindfolded.csv")
Wilson_2017_rt_2 <- read_sav("../data_files/Wilson_2017_rt_2.sav") #verified by repro anova
Wilson_2017_er_2 <- read_sav("../data_files/Wilson_2017_er_2.sav") # verified by repro anova
#this is actually just duplicate ex2 rt data Wilson_2017_rt_3 <- read_sav("../data_files/Wilson_2017_rt_3.sav")
Wilson_2017_er_4 <- read_sav("../data_files/Wilson_2017_er_3.sav") #verified by repro anova



#Ex1 (no-blindfold data)
#add subject and measure info
wilson_2017_er_1 <- wilson_2017_er_1 %>% 
  rowid_to_column("Subject") %>% 
  mutate(RTorError = "Error")
wilson_2017_rt_1 <- wilson_2017_rt_1 %>% 
  rowid_to_column("Subject") %>% 
  mutate(RTorError = "RT", Subject = Subject + 100) #make subjects unique from er subjects

Wilson_2017_1 <- rbind(wilson_2017_er_1, wilson_2017_rt_1)

#no blindfold wide to long
Wilson_2017_1 <- Wilson_2017_1 %>%
  gather(key = Condition, value = Value, OtherCons:SelfIncons) %>% 
  mutate(
    Matching = "match", #add match column (only have match data for ex1 blindfold)
    Condition = paste(Condition, "NoBlind", sep = ""),
    Subject = paste("Wilson_2017_1_NoBlind", Subject, sep = "_")
    ) 

#Ex1 blindfold, remove fillers
wilson_2017_1_blind <- wilson_2017_1_blind %>% 
  filter(as.character(myfactor) != "Filler")

#Calculate mean rt by subj for blindfold ex1
wilson_2017_1_blind_rt <- wilson_2017_1_blind %>% 
  select(-myfactor, -X) %>% 
  filter(myerror == 0) %>% 
  group_by(subj) %>% 
  filter(reactime >= (mean(reactime)-2.5*sd(reactime)) & reactime <= (mean(reactime)+2.5*sd(reactime))) %>% #filter to 2.5 SD from mean
  group_by(subj, fPerspective, fconsistency, frightansw) %>% 
  summarise(Value = mean(reactime)) %>% 
  mutate(RTorError = "RT")
  
#Calculate mean er by subj for blindfold ex1 
wilson_2017_1_blind_er <- wilson_2017_1_blind %>% 
  select(-myfactor, -X) %>% 
  group_by(subj, fPerspective, fconsistency, frightansw) %>% 
  summarise(Value = mean(myerror)) %>% 
  mutate(RTorError = "Error")

wilson_2017_1_blind <- rbind(wilson_2017_1_blind_er, wilson_2017_1_blind_rt)

#prepare blindfold to merge with no-blindfold
wilson_2017_1_blind <- wilson_2017_1_blind %>% 
  mutate(
    Subject = paste("Wilson_2017_1_blind", str_extract(as.character(subj), "\\d"), sep= "_"),
    Condition = paste(fPerspective, case_when(fconsistency=="Consistent" ~ "Cons",fconsistency=="Inconsistent" ~ "Incons"), "Blind", sep = ""),
    Matching = case_when(frightansw=="Yes" ~ "match", frightansw=="No" ~ "mismatch")
    ) %>% 
  ungroup() %>% 
  select(-c(subj, fPerspective, fconsistency, frightansw))

#combine blindfold and no-blindfold
Wilson_2017_1 <- rbind(wilson_2017_1_blind, Wilson_2017_1)

#change colnames and coding names
Wilson_2017_1 <- Wilson_2017_1 %>% 
  mutate(
    Paper = "Wilson_2017",
    Condition = paste(Condition,"Ex1", sep = "_"),
    StudyNumber = 1,
    Consistency = case_when(grepl("Cons", Condition)~ "con", grepl("Incons", Condition) ~ "incon"),
    SelfOther = case_when(grepl("Self", Condition) ~ "self", grepl("Other", Condition) ~ "other"),
    Sex = NA,
    Age = NA
  )


#Ex2
#subject and measure columns
Wilson_2017_rt_2 <- Wilson_2017_rt_2 %>% 
  rowid_to_column("Subject") %>% 
  mutate(RTorError = "RT")
Wilson_2017_er_2 <-Wilson_2017_er_2 %>% 
  rowid_to_column("Subject") %>% 
  mutate(RTorError = "Error", Subject = Subject+100) %>% #make subjects unique from RTdata
  rename(InterfStrength = interfStrength) #fix weird naming issue

Wilson_2017_2 <- rbind(Wilson_2017_rt_2, Wilson_2017_er_2)

#wide to long
Wilson_2017_2 <- Wilson_2017_2 %>% 
  gather(key = Condition, value = Value, SelfCons:OtherIncons) %>% 
  select(-InterfStrength) #remove unnecessary column

#change colnames and coding scheme
Wilson_2017_2 <- Wilson_2017_2 %>% 
  mutate(
    Cue = case_when(Cue == 1 ~ "camera", Cue == 2 ~ "arrow", Cue == 3 ~ "avatar"),
    Condition = paste(Condition, Cue, "Ex2", sep = "_"),
    Matching = "match",
    Paper = "Wilson_2017",
    StudyNumber = 2,
    Consistency = case_when(grepl("Cons", Condition)~ "con", grepl("Incons", Condition) ~ "incon"),
    SelfOther = case_when(grepl("Self", Condition) ~ "self", grepl("Other", Condition) ~ "other"),
    Subject = paste("Wilson_2017_ex2", Subject, sep = "_"),
    Sex = NA,
    Age = NA
  ) %>% 
  select (-Cue)

#REPRODUCE STATS FROM PAPER
# test_21 <- Wilson_2017_2 %>% filter(Cue == 1, RTorError == "RT")
# test_plot1 <- plot_ly(test_21) %>% 
#   add_trace(
#     type = "bar",
#     x = ~SelfOther,
#     y = ~Value,
#     split = ~Consistency
#   )
# test_22 <- Wilson_2017_2 %>% filter(Cue == 2, RTorError == "RT")
# test_plot2 <- plot_ly(test_22) %>% 
#   add_trace(
#     type = "bar",
#     x = ~SelfOther,
#     y = ~Value,
#     split = ~Consistency
#   )
# test_23 <- Wilson_2017_2 %>% filter(Cue == 3, RTorError == "RT")
# test_plot3 <- plot_ly(test_23) %>% 
#   add_trace(
#     type = "bar",
#     x = ~SelfOther,
#     y = ~Value,
#     split = ~Consistency
#   )
# test_plot1
# test_plot2
# test_plot3
# subplot(test_plot1, test_plot2, test_plot3)
# 
# 
# Wilson_2017_2.err <- filter(Wilson_2017_2, RTorError == "Error")
# aov_test <- anova_test(Wilson_2017_2.err, wid = Subject, dv = Value, between = Cue, within = c(SelfOther, Consistency))
# aov_test
# #Ex3 - this has duplicate data from ex2--exclude
# #subject and measure columns
Wilson_2017_er_4 <- Wilson_2017_er_4 %>%
  rowid_to_column("Subject") %>%
  mutate(RTorError = "Error")
# Wilson_2017_rt_3 <-Wilson_2017_rt_3 %>% 
#   rowid_to_column("Subject") %>% 
#   mutate(RTorError = "RT", Subject = Subject+100) %>% #make subjects unique from RTdata
#   rename(InterfStrength = interfStrenght) #fix weird naming issue
# 
#wide to long
Wilson_2017_4 <- Wilson_2017_er_4%>%
   gather(key = Condition, value = Value, OtherCons:SelfIncons) %>%
  rename(Cue = cue) %>%
   select(-InterfStrength)
 # Wilson_2017_rt_3 <- Wilson_2017_rt_3 %>%
 #   gather(key = Condition, value = Value, CSelfCons:CSelfIncons) %>%
 #   select(-InterfStrength)

# Wilson_2017_3 <- rbind(Wilson_2017_rt_3, Wilson_2017_er_3)

 #change colnames and coding scheme
 Wilson_2017_4 <- Wilson_2017_4 %>%
   mutate(
     Cue = case_when(Cue == 1 ~ "camera", Cue == 2 ~ "arrow", Cue == 3 ~ "avatar"),
     Condition = paste(Condition, Cue, "Ex4", sep = "_"),
     Matching = "match",
     Paper = "Wilson_2017",
     StudyNumber = 4,
     Consistency = case_when(grepl("Cons", Condition)~ "con", grepl("Incons", Condition) ~ "incon"),
    SelfOther = case_when(grepl("Self", Condition) ~ "self", grepl("Other", Condition) ~ "other"),
     Subject = paste("Wilson_2017_ex4", Subject, sep = "_"),
     Sex = NA,
     Age = NA
   ) %>% 
   select (-Cue)

#EXCLUDE EX1(add only ex2 and ex4 to dataframe)
Wilson_2017 <- rbind(Wilson_2017_2, Wilson_2017_4)

#merge with classification df
Wilson_2017_Codes <- filter(classifications, Paper == "Wilson_2017") 

Wilson_2017  <- left_join(Wilson_2017, Wilson_2017_Codes)
```

##Bukowski 2017
```{r}
Bukowski_2017 <- read_sav("../data_files/Bukowski_2017.sav")

Bukowski_2017 <- Bukowski_2017 %>% 
  mutate(
    Condition = case_when(EmonumCorrected == -3 ~ "Sadness", EmonumCorrected == 0 ~ "Control", EmonumCorrected == 3 ~ "Happiness", TRUE ~ Condition)
    ) %>% 
  filter(!is.na(EmonumCorrected))

#select cols
Bukowski_2017 <- Bukowski_2017 %>% 
  select(expnum:expname, Gendernum, Age, ACC_mean_Other_Con, ACC_mean_Other_Incon, ACC_mean_Self_Con, ACC_mean_Self_Incon, ControlOrNOT, FLTSD_Other_Con_RT_mean, FLTSD_Other_Incon_RT_mean,FLTSD_Self_Con_RT_mean,FLTSD_Self_Incon_RT_mean, Condition) %>% 
  rowid_to_column("Subject")

#wide to long
Bukowski_2017 <- Bukowski_2017 %>% 
  gather(key = condition, value = Value, c(ACC_mean_Other_Con:ACC_mean_Self_Incon, FLTSD_Other_Con_RT_mean:FLTSD_Self_Incon_RT_mean))

Bukowski_2017 <- Bukowski_2017 %>% 
  mutate(
    Paper = "Bukowski_2017",
    expnum = case_when(expnum==7~2, TRUE~expnum), #exp2 mislabeled as exp7
    Consistency = case_when(grepl("Con", condition)~"Con", grepl("Incon", condition)~"Incon"),
    SelfOther = case_when(grepl("Other", condition)~"other", grepl("Self", condition)~"self"),
    Condition = paste(
      Condition, 
      case_when(SelfOther=="other"~"Other", SelfOther=="self"~"Self"),
      Consistency,
      expnum, sep = "_"),
    StudyNumber = expnum,
    Sex = case_when(Gendernum == -1 ~ "F", Gendernum == 1 ~ "M"), #confirmed from participant stats
    RTorError = case_when(grepl("ACC",condition) ~ "Error", grepl("RT", condition) ~ "RT"),
    Matching = "match",
    Subject = paste("Bukowski_2017", Subject, sep = "_"),
    Consistency = case_when(Consistency=="Con"~"con", Consistency=="Incon"~"incon"),
    Value = case_when(RTorError=="Error"~1-Value, TRUE~Value) #Change acc rate to err
  ) %>% 
  select(-expnum, -Gendernum, -Experimenter, -condition, -expname, -ControlOrNOT, -EmonumCorrected)


#merge with classification df
Bukowski_2017_Codes <- filter(classifications, Paper == "Bukowski_2017")
Bukowski_2017 <- left_join(Bukowski_2017, Bukowski_2017_Codes)
```


##Santiesteban 2017
```{r}
Santiesteban_2017_1 <- read_sav("../data_files/Santiesteban_2017_1.sav")
Santiesteban_2017_2 <- read_sav("../data_files/Santiesteban_2017_2.sav")

#Ex1
#select cols
Santiesteban_2017_1 <- Santiesteban_2017_1 %>% 
  select(-Cbal, -handedness) %>% 
  mutate(
    Subject = paste("Santiesteban_2017_ex1", SubID, sep ="_"), 
    StudyNumber = 1) %>% 
  select(-SubID)

#wide to long
Santiesteban_2017_1 <- Santiesteban_2017_1 %>% 
  gather(key = Condition, value = Value, ArrowRTSelfC:AvatarRTOtherI)

#Ex2
#SelectCols
Santiesteban_2017_2 <-Santiesteban_2017_2 %>% 
  select(-handedness, -CbalAvArr, -CbalTPJMOfirst) %>% 
  rowid_to_column("Subject") %>% 
  mutate(
    Subject = paste("Santiesteban_2017_ex2", Subject, sep ="_"),
    StudyNumber = 2)

#wide to long
Santiesteban_2017_2 <- Santiesteban_2017_2 %>% 
  gather(key = Condition, value = Value, TPJArrowRTSelfC:MOCAvatarRTOtherI)

Santiesteban_2017 <-rbind(Santiesteban_2017_1, Santiesteban_2017_2)

#wrangle columns
Santiesteban_2017 <- Santiesteban_2017 %>% 
  mutate(
    Paper="Santiesteban_2017",
    Sex = case_when(gender == 0 ~ "F", gender == 1 ~ "M"),
    RTorError = "RT", #only have RT
    Matching = "mixed",
    Consistency = case_when(grepl("C", Condition)~"con", grepl("I", Condition)~"incon"),
    SelfOther = case_when(
      grepl("Self", Condition) ~ "self", 
      grepl("RTArrow", Condition) ~ "other", 
      grepl("RTOther", Condition) ~ "other")
  ) %>% 
  rename(Age = age) %>% 
  select(-gender)
  

#merge with classification df
Santiesteban_2017_Codes <- filter(classifications, Paper == "Santiesteban_2017")
Santiesteban_2017 <- left_join(Santiesteban_2017, Santiesteban_2017_Codes)
```


##Schwarzkopf 2014
```{r}
Schwarzkopf_2014 <- read.csv("../data_files/Schwarzkopf_2014.csv")

#remove unneeded columns
Schwarzkopf_2014 <-Schwarzkopf_2014 %>% 
  select(-VAR00009, -VAR00018, -VAR00019, -VAR00020, -VAR00021, -VAR00013, -filter_., -Item, -COT,-HandednessL1R2, -time, -BlockOrder)

#remove practice and filler trials
Schwarzkopf_2014 <-Schwarzkopf_2014 %>% 
  filter(
    BlockNumber != 0, #practice
    consistFiller0Yes1No2 != 0 #filler
  ) %>% 
  select(-BlockNumber)

#wrangle into usable format
Schwarzkopf_2014 <-Schwarzkopf_2014 %>% 
  mutate(
    Sex = case_when(GenderF1M2==1~"F", GenderF1M2==2~"M"),
    Condition = paste(code, case_when(Autist1Control2==1~"ASD", Autist1Control2==2~"Control"), sep = "_"),
    Matching = case_when(matchMatch1Mismatch2==1~"match", matchMatch1Mismatch2==2~"mismatch"),
    Consistency = case_when(consistFiller0Yes1No2==1~"con", consistFiller0Yes1No2==2~"incon"),
    SelfOther = case_when(perspSelf1Other2==1~"self", perspSelf1Other2==2~"other")
  ) %>% 
  select(-c(GenderF1M2, code, Autist1Control2, matchMatch1Mismatch2, consistFiller0Yes1No2, perspSelf1Other2))

#average rt at perticipant x condition level
Schwarzkopf_2014_rt <-Schwarzkopf_2014 %>% 
  filter(Accuracy == 1) %>% #remove incorrect trials
  group_by(SubjectNumber) %>% 
  filter(RT >= (mean(RT)-2.5*sd(RT)) & RT <= (mean(RT)+2.5*sd(RT))) %>% #filter to 2.5 SD from mean
  group_by(SubjectNumber, Sex, Condition, Matching, Consistency, SelfOther) %>% 
  summarise(Value = mean(RT)) %>% 
  mutate(RTorError = "RT")

#average er at perticipant x condition level
Schwarzkopf_2014_er <-Schwarzkopf_2014 %>% 
  group_by(SubjectNumber, Sex, Condition, Matching, Consistency, SelfOther) %>% 
  summarise(Value = mean(Accuracy)) %>% 
  mutate(RTorError = "Error")

Schwarzkopf_2014 <- rbind(Schwarzkopf_2014_er, Schwarzkopf_2014_rt)

#add few more columns
Schwarzkopf_2014 <- Schwarzkopf_2014 %>% 
  mutate(
    Age = NA,
    Paper = "Schwarzkopf_2014",
    StudyNumber = 1,
    Subject = paste("Schwarzkopf_2014", SubjectNumber, sep = "_")
  ) %>% 
  ungroup() %>% 
  select(-SubjectNumber) %>% 
  mutate(
    Value = case_when(RTorError=="Error"~1-Value, TRUE~Value)#change acc rate to err rate
  )

#merge with coding df
Schwarzkopf_2014_Codes <- filter(classifications, Paper == "Schwarzkopf_2014")
 
Schwarzkopf_2014  <- left_join(Schwarzkopf_2014, Schwarzkopf_2014_Codes)
```



##Schurz_2015
```{r}
Schurz_2015 <- read.csv("../data_files/Schurz_2015.csv")

#select cols 
Schurz_2015 <- Schurz_2015 %>% 
  select(-S_ArrowAll_RT, -S_BlockAll_RT)
#wide to long
Schurz_2015 <- Schurz_2015 %>% 
  gather(key = Condition, value = Value, S_AvatarCons_RT:S_BlockIncons_Inc)

#add and reorganize columns
Schurz_2015 <- Schurz_2015 %>% 
  mutate(
    SelfOther = case_when(
      substr(Condition, 1, 1)=="S" ~ "self",
      substr(Condition, 1, 1)=="O" ~ "other"),
    Consistency = case_when(
      grepl("Cons", Condition) ~ "con",
      grepl("Incons", Condition) ~ "incon"),
    RTorError = case_when(
      grepl("RT", Condition) ~ "RT",
      TRUE ~ "Error"),
    StudyNumber = 1,
    Matching = NA, #was a "report" task (not "verify")
    Age = NA,
    Sex = NA,
    Paper = "Schurz_2015",
    Subject = paste("Schurz_2015", Subject, sep = "_")
  )

#change percent error /100 to error rate /1
Schurz_2015 <- Schurz_2015 %>% 
  mutate(
    Value = case_when(RTorError=="Error" ~ Value/100, TRUE ~ Value)
  )

#merge with coding df
Schurz_2015_Codes <- filter(classifications, Paper == "Schurz_2015")

Schurz_2015  <- left_join(Schurz_2015, Schurz_2015_Codes)
```

##Westra 2021
```{r}
Westra_2021_1 <- read.csv("../data_files/Westra_2021_ex1.csv")#one used in analyses (ALREADY CLEANED and filtered)
Westra_2021_2 <- read.csv("../data_files/Westra_2021_ex2.csv")
Westra_2021_3 <- read.csv("../data_files/Westra_2021_ex3.csv")
Westra_2021_4 <- read.csv("../data_files/Westra_2021_ex4.csv")

#EX1
#average rt
Westra_2021_1_rt <- Westra_2021_1 %>% 
  filter(Correct == 1) %>% 
  group_by(Age, SubjectId, Sex, Perspective, Inconsistent) %>% 
  summarise(Value = mean(RT)) %>% 
  mutate(
    Animate = 1,
    Subject = paste("Westra_2021", "ex1", SubjectId, sep = "_"),
    StudyNumber = 1,
    RTorError = "RT") %>% 
  ungroup() %>% 
  select(-SubjectId)
#average er
Westra_2021_1_er <- Westra_2021_1 %>% 
  group_by(Age, SubjectId, Sex, Perspective, Inconsistent) %>% 
  summarise(Value = 1 - mean(Correct)) %>% 
  mutate(
    Animate = 1,
    Subject = paste("Westra_2021", "ex1", SubjectId, sep = "_"),
    StudyNumber = 1,
    RTorError = "Error"
  ) %>% 
  ungroup() %>% 
  select(-SubjectId)

#Ex2
#average rt
Westra_2021_2_rt <- Westra_2021_2 %>% 
  filter(Correct == 1) %>% 
  group_by(Age, SubjectId, Sex, Perspective, Inconsistent, Animate) %>% 
  summarise(Value = mean(RT)) %>% 
  mutate(
    Subject = paste("Westra_2021", "ex2", SubjectId, sep = "_"),
    StudyNumber = 2,
    RTorError = "RT"
  ) %>% 
  ungroup() %>% 
  select(-SubjectId)

#average er
Westra_2021_2_er <- Westra_2021_2 %>% 
  group_by(Age, SubjectId, Sex, Perspective, Inconsistent, Animate) %>% 
  summarise(Value = 1-mean(Correct)) %>% 
  mutate(
    Subject = paste("Westra_2021", "ex2", SubjectId, sep = "_"),
    StudyNumber = 2,
    RTorError = "Error"
  ) %>% 
  ungroup() %>% 
  select(-SubjectId)

#Ex3
#average rt
Westra_2021_3_rt <- Westra_2021_3 %>% 
  filter(Correct == 1) %>% 
  group_by(Age, SubjectId, Sex, Perspective, Inconsistent) %>% 
  summarise(Value = mean(RT)) %>% 
  mutate(
    StudyNumber = 3,
    RTorError = "RT",
    Animate = 0,
    Subject = paste("Westra_2021", "ex3", SubjectId, sep = "_")
  ) %>% 
  ungroup() %>% 
  select(-SubjectId)

#average er
Westra_2021_3_er <- Westra_2021_3 %>% 
  group_by(Age, SubjectId, Sex, Perspective, Inconsistent) %>% 
  summarise(Value = 1-mean(Correct)) %>% 
  mutate(
    StudyNumber = 3,
    RTorError = "Error",
    Animate = 0,
    Subject = paste("Westra_2021", "ex3", SubjectId, sep = "_")
  ) %>% 
  ungroup() %>% 
  select(-SubjectId)

#Ex4
#average rt
Westra_2021_4_rt <- Westra_2021_4 %>% 
  filter(correct == 1) %>% 
  group_by(Age, SubjectId, Sex, Consistency, Animacy) %>% 
  summarise(Value = 1000*mean(RT)) %>% #convert sec to ms
  mutate(
    StudyNumber = 4,
    RTorError = "RT",
    Perspective = "Self",
    Animate = case_when(Animacy=="Animate" ~ 1, Animacy=="Inanimate" ~ 0),
    Inconsistent = case_when(Consistency=="Consistent"~"Consistent", Consistency=="Inconsistent"~"Inconsistent"),
    Subject = paste("Westra_2021", "ex4", SubjectId, sep = "_")
  ) %>% 
  ungroup() %>% 
  select(-Animacy, -Consistency, -SubjectId)

#average er
Westra_2021_4_er <- Westra_2021_4 %>% 
  group_by(Age, SubjectId, Sex, Consistency, Animacy) %>% 
  summarise(Value = 1 - mean(correct)) %>% 
  mutate(
    StudyNumber = 4,
    Perspective = "Self",
    RTorError = "Error",
    Animate = case_when(Animacy=="Animate" ~ 1, Animacy=="Inanimate" ~ 0),
    Inconsistent = case_when(Consistency=="Consistent"~"Consistent", Consistency=="Inconsistent"~"Inconsistent"),
    Subject = paste("Westra_2021", "ex4", SubjectId, sep = "_")
  ) %>% 
  ungroup() %>% 
  select(-Animacy, -Consistency, -SubjectId)

Westra_2021_1_er$Subject <- as.character(Westra_2021_1_er$Subject)

#combine studies
Westra_2021_data <- rbind(
  Westra_2021_1_er,
  Westra_2021_1_rt,
  Westra_2021_2_er,
  Westra_2021_2_rt,
  Westra_2021_3_er,
  Westra_2021_3_rt,
  Westra_2021_4_er,
  Westra_2021_4_rt
)

#make congruent w/ format
Westra_2021_data <- Westra_2021_data %>% 
  ungroup() %>% 
  mutate(
    SelfOther = case_when(Perspective=="Self"~"self", Perspective=="Other"~"other"),
    Consistency = case_when(Inconsistent=="Consistent"~ "con", Inconsistent=="Inconsistent"~"incon"),
    Matching = "match",
    Condition = paste(
      SelfOther, 
      Consistency, 
      case_when(Animate==1~"animate", Animate==0~"inanimate"),
      StudyNumber, sep = "_")
  ) %>% 
  select(-Perspective, -Animate, -Inconsistent)

#get classifications
Westra_2021_codes <- filter(classifications, Paper == "Westra_2021")

#add to classification data
Westra_2021 <- left_join(
  Westra_2021_data,
  Westra_2021_codes
)
```

#Merge data (S. Shin)
```{r}

#merge data
Samson_final <- rbind(
  Qureshi_2018,
  Qureshi_2020,
  Gardner_B_2018_2,
  Drayton_2018,
  Deliens_2018,
  Deroualle_2017,
  Todd_2019,
  Todd_2020,
  Doi_2020,
  Simonsen_2020,
  Ramsey_2013,
  Pavlidou_2018, 
  Pavlidou_2019,
  Cole_2016,
  McCleery_2011,
  Surtees_2012,
  Wilson_2017,
  Bukowski_2017,
  Santiesteban_2017,
  Schurz_2015
)


#fix a couple column types, and merge problematic data
Schwarzkopf_2014$Age<- as.numeric(Schwarzkopf_2014$Age)
Surtees_2016$Sex <- as.character(Surtees_2016$Sex)
Surtees_2016$Age<- as.numeric(Surtees_2016$Age)
OGrady_2017$Age <- as.numeric(OGrady_2017$Age)
Wang_2019$Sex <- as.character(Wang_2019$Sex)
Wang_2019$Age<- as.numeric(Wang_2019$Age)

Samson_final <- Samson_final %>% bind_rows(
  Marshall_2018,
  OGrady_2017,
  Wang_2019,
  Saether_2021,
  Surtees_2016,
  Langton_2018,
  Schwarzkopf_2014,
  Westra_2021
)

#fix a couple naming things to merge with other dataset
Samson_final <- Samson_final %>% 
  mutate(
    Consistent = case_when(
      Consistency=="con" ~ 1,
      Consistency=="incon" ~ 0
    ),
    PerspectiveSelf = case_when(
      SelfOther=="self" ~ 1,
      SelfOther=="other" ~ 0
    )
  ) %>% 
  ungroup() %>% 
  select(-c(Consistency, SelfOther))
Samson_final
```

#Data Cleaning (C. Holland)

##Furlanetto 2016 

###Reaction Time 
```{r}
Furlanetto_2016_RT1 <- read_sav("../data_files/Furlanetto_2016_RT1.sav")

#Wide to Long
Furlanetto_2016_RT1 <- Furlanetto_2016_RT1 %>% add_column(Subject = 1:17)
Furlanetto_2016_RT1$Subject <- factor(Furlanetto_2016_RT1$Subject)
Furlanetto_Long <- gather(Furlanetto_2016_RT1, Condition, reaction_time, eyes_self_con:non_seeing_other_incon, factor_key=TRUE)
# Furlanetto_Long

#reference
# colnames(Furlanetto_Long)

#Add Dimensions of Interest
Furlanetto_Long<- Furlanetto_Long %>% rename(Value = reaction_time)
Furlanetto_Long <- Furlanetto_Long  %>% add_column(Paper = 'Furlanetto_2016')
Furlanetto_Long <- Furlanetto_Long  %>% add_column(RTorError = 'RT')
Furlanetto_Long <- Furlanetto_Long  %>% add_column(Sex = NA)
Furlanetto_Long <- Furlanetto_Long  %>% add_column(Age = NA)
Furlanetto_Long <- Furlanetto_Long %>%  add_column(StudyNumber = '1')

#Add Consistent Column
Furlanetto_Long <- Furlanetto_Long  %>% 
  mutate(Consistent = case_when(Condition == "eyes_self_con" ~ 1, 
                                Condition == "eyes_other_con" ~ 1, 
                                Condition == "seeing_self_con" ~ 1,
                                Condition ==  "seeing_other_con" ~ 1, 
                                Condition ==  "non_seeing_self_con" ~1, 
                                Condition ==  "non_seeing_other_con" ~1,
                                Condition == "eyes_self_incon" ~ 0, 
                                Condition == "eyes_other_incon"~ 0,
                                Condition == "seeing_self_incon"~ 0 , 
                                Condition ==  "seeing_other_incon" ~ 0,
                                Condition == "non_seeing_self_incon"~ 0, 
                                Condition ==  "non_seeing_other_incon"~ 0))
Furlanetto_Long <- Furlanetto_Long  %>% 
  mutate(PerspectiveSelf = case_when(Condition == "eyes_self_con" ~ 1,
                                Condition == "eyes_other_con" ~ 0, 
                                Condition == "seeing_self_con" ~ 1,
                                Condition ==  "seeing_other_con" ~ 0, 
                                Condition ==  "non_seeing_self_con" ~1, 
                                Condition ==  "non_seeing_other_con" ~0,
                                Condition == "eyes_self_incon" ~ 1, 
                                Condition == "eyes_other_incon"~ 0,
                                Condition == "seeing_self_incon"~ 1 , 
                                Condition ==  "seeing_other_incon" ~ 0,
                                Condition == "non_seeing_self_incon"~ 1, 
                                Condition ==  "non_seeing_other_incon"~ 0))

Furlanetto_Long <- Furlanetto_Long[, c(4,8,7,6,1,2,9,10,5,3)]

Furlanetto_Long$Subject <- paste(Furlanetto_Long$Subject,"Fur16_RT",sep="_")

```

Furlanetto 2016 
###Error Rate 
```{r}
Furlanetto_2016_Error1 <- read_sav("../data_files/Furlanetto_2016_Error1.sav")
Furlanetto_2016_Error1  <- Furlanetto_2016_Error1 %>% add_column(Subject = 1:17)
Furlanetto_2016_Error1$Subject <- factor(Furlanetto_2016_Error1$Subject)
Furlanetto_Long_Error <- gather(Furlanetto_2016_Error1, Condition, error_rate, eyes_self_con:non_seeing_other_incon, factor_key=TRUE)
# Furlanetto_Long_Error

#Add Dimensions of Interest
Furlanetto_Long_Error <- Furlanetto_Long_Error %>% rename(Value = error_rate)
Furlanetto_Long_Error$Value <- Furlanetto_Long_Error$Value / 100
Furlanetto_Long_Error <- Furlanetto_Long_Error  %>% add_column(Paper = 'Furlanetto_2016')
Furlanetto_Long_Error  <- Furlanetto_Long_Error   %>% add_column(RTorError = 'Error')
Furlanetto_Long_Error  <- Furlanetto_Long_Error   %>% add_column(StudyNumber = '1')

#Create Perspective and Consistency Column
Furlanetto_Long_Error <- Furlanetto_Long_Error  %>% 
  mutate(Consistent = case_when(Condition == "eyes_self_con" ~ 1, 
                                Condition == "eyes_other_con" ~ 1, 
                                Condition == "seeing_self_con" ~ 1,
                                Condition ==  "seeing_other_con" ~ 1, 
                                Condition ==  "non_seeing_self_con" ~1, 
                                Condition ==  "non_seeing_other_con" ~1,
                                Condition == "eyes_self_incon" ~ 0, 
                                Condition == "eyes_other_incon"~ 0,
                                Condition == "seeing_self_incon"~ 0 , 
                                Condition ==  "seeing_other_incon" ~ 0,
                                Condition == "non_seeing_self_incon"~ 0, 
                                Condition ==  "non_seeing_other_incon"~ 0))
Furlanetto_Long_Error <- Furlanetto_Long_Error  %>% 
  mutate(PerspectiveSelf = case_when(Condition == "eyes_self_con" ~ 1,
                                Condition == "eyes_other_con" ~ 0, 
                                Condition == "seeing_self_con" ~ 1,
                                Condition ==  "seeing_other_con" ~ 0, 
                                Condition ==  "non_seeing_self_con" ~1, 
                                Condition ==  "non_seeing_other_con" ~0,
                                Condition == "eyes_self_incon" ~ 1, 
                                Condition == "eyes_other_incon"~ 0,
                                Condition == "seeing_self_incon"~ 1 , 
                                Condition ==  "seeing_other_incon" ~ 0,
                                Condition == "non_seeing_self_incon"~ 1, 
                                Condition ==  "non_seeing_other_incon"~ 0))

#Recode Sex as MF instead of 01 0 == Male, 1 == F
Furlanetto_Long_Error <- Furlanetto_Long_Error %>% rename(Sex = SEX)
Furlanetto_Long_Error <- Furlanetto_Long_Error %>% rename(Age = AGE)

Furlanetto_Long_Error <- Furlanetto_Long_Error %>%  
  mutate(Sex = recode(Sex, '0' = "M", 
                           '1'= "F") )

Furlanetto_Long_Error <- Furlanetto_Long_Error[, c(6,8,2,1,3,4,9,10,7,5)]

Furlanetto_Long_Error$Subject <- paste(Furlanetto_Long_Error$Subject,"Fur16_ER",sep="_")

```

###Combine + Data Merge
```{r}
Fur16 <- rbind(Furlanetto_Long, Furlanetto_Long_Error)
Fur16$RTorError <- factor(Fur16$RTorError)
```

##Todd 2016 
###Exp1
```{r}
Todd_2016_Exp1 <- read_sav("../data_files/Todd_2016_Exp1.sav")
Todd_2016_Exp1 <- Todd_2016_Exp1 %>% rename(filterdata = `filter_$`)
Todd_2016_Exp1 <- subset(Todd_2016_Exp1, filterdata==1)

Todd_2016_Exp1$Subject <- factor(Todd_2016_Exp1$Subject) #need to factor subject before gathering
Todd_2016_Exp1<- Todd_2016_Exp1[ , c("Subject", "emotion", "subsex", "OC_RT","OI_RT", "SC_RT", "SI_RT", "OC_error","OI_error","SC_error","SI_error" )]


Todd_2016_Exp1 <- Todd_2016_Exp1 %>%  mutate(subsex = coalesce(subsex, 0)) #get rid of na's so that you can gather
Todd_2016_Exp1<- Todd_2016_Exp1  %>% 
  mutate(subsex= case_when(subsex == 1 ~ "M", 
                        subsex == 2  ~ "F",
                        subsex == 0 ~ "Unreported")) 
Todd_2016_Exp1$subsex <- factor(Todd_2016_Exp1$subsex)#need to factor subsex before gathering

#Wide to Long
Todd_2016_Exp1_Long <- gather(Todd_2016_Exp1, Condition, reaction_time, OC_RT:SI_error, factor_key=TRUE) 

#Add Dimensions of Interest
Todd_2016_Exp1_Long <- Todd_2016_Exp1_Long %>% rename(Sex = subsex)
Todd_2016_Exp1_Long <- Todd_2016_Exp1_Long %>% rename(Value = reaction_time)
Todd_2016_Exp1_Long <- Todd_2016_Exp1_Long %>% add_column(Paper = 'Todd_2016')
Todd_2016_Exp1_Long <- Todd_2016_Exp1_Long %>% add_column(Age = NA)
Todd_2016_Exp1_Long <- Todd_2016_Exp1_Long %>% add_column(StudyNumber = '1')


#Create Consistency and Perspective Column
Todd_2016_Exp1_Long<- Todd_2016_Exp1_Long  %>% 
  mutate(Consistent = case_when(Condition == "OC_RT"  ~ 1,
                                Condition ==  "OI_RT"  ~ 0, 
                                Condition ==  "SC_RT"  ~1, 
                                Condition ==  "SI_RT"   ~0,
                                Condition == "OC_error"  ~ 1, 
                                Condition == "OI_error" ~ 0,
                                Condition == "SC_error" ~ 1 , 
                                Condition ==  "SI_error"  ~ 0))
#NOTE: Consistent Levels { 0 = Inconsistent, 1= Consistent, 2 = Subject Mean Across I+C}

Todd_2016_Exp1_Long<- Todd_2016_Exp1_Long  %>% 
  mutate(PerspectiveSelf = case_when(Condition == "OC_RT"  ~ 0,
                                Condition ==  "OI_RT"  ~ 0, 
                                Condition ==  "SC_RT"  ~1, 
                                Condition ==  "SI_RT"   ~1,
                                Condition == "OC_error"  ~ 0, 
                                Condition == "OI_error" ~ 0,
                                Condition == "SC_error" ~ 1 , 
                                Condition ==  "SI_error"  ~ 1))
#NOTE: Consistent Levels { 0 = Other, 1= Self, 2 = Subject Mean Across Self+Other}

#Create RTorError Column 
Todd_2016_Exp1_Long<- Todd_2016_Exp1_Long  %>% 
  mutate(RTorError = case_when(Condition == "OC_RT"  ~ "RT",
                                Condition ==  "OI_RT"  ~ "RT", 
                                Condition ==  "SC_RT"  ~"RT", 
                                Condition ==  "SI_RT"   ~"RT",
                                Condition == "OC_error"  ~ "Error", 
                                Condition == "OI_error" ~ "Error",
                                Condition == "SC_error" ~ "Error" , 
                                Condition ==  "SI_error"  ~ "Error"))


#Recode Condition
Todd_2016_Exp1_Long<- Todd_2016_Exp1_Long %>% 
  mutate(emotion= case_when(emotion == 1 ~ "anxiety", 
                        emotion == 2  ~ "neutral")) 
#Combine emotion and Condition into one name 
Todd_2016_Exp1_Long$Condition <- paste(Todd_2016_Exp1_Long$Condition, Todd_2016_Exp1_Long$emotion, sep="_")
Todd_2016_Exp1_Long <- Todd_2016_Exp1_Long[-c(2)] #Remove column so you end up with 10 clean columns

#Make Subjects and Condition Unique
Todd_2016_Exp1_Long$Condition <- paste("Tod16_Ex1", Todd_2016_Exp1_Long$Condition,sep="_")
Todd_2016_Exp1_Long$Subject <- paste(Todd_2016_Exp1_Long$Subject,"Tod16_Ex1",sep="_")


Todd_2016_Exp1_Long <- Todd_2016_Exp1_Long[, c(5,7,6,2,1,3,8,9,10,4)]
Todd_2016_Exp1_Long$Condition <- factor(Todd_2016_Exp1_Long$Condition)

```

Todd 2016 
###Exp 2a
```{r}
Todd_2016_Exp2a <- read_sav("../data_files/Todd_2016_Exp2a_CH.sav")
Todd_2016_Exp2a <- Todd_2016_Exp2a  %>% rename(filterdata = `filter_$`) #renamed because throwing weird errors with the dollar sign
Todd_2016_Exp2a <- subset(Todd_2016_Exp2a , filterdata==1) #take only the data thats less than 30% error rate. This exclusion criteria was used by Todd too but the bad subjects were still included in the data we received. 

Todd_2016_Exp2a$Subject <- factor(Todd_2016_Exp2a$Subject)
Todd_2016_Exp2a <- Todd_2016_Exp2a[ , c("Subject", "subsex","emotion", "OC_RT","OI_RT", "SC_RT", "SI_RT", "OC_error","OI_error","SC_error","SI_error")]

Todd_2016_Exp2a<- Todd_2016_Exp2a  %>% 
  mutate(subsex= case_when(subsex == 1 ~ "M", 
                        subsex == 2  ~ "F")) 
Todd_2016_Exp2a$subsex <- factor(Todd_2016_Exp2a$subsex)

#Wide to Long
Todd_2016_Exp2a_Long <- gather(Todd_2016_Exp2a, Condition, reaction_time, OC_RT:SI_error, factor_key=TRUE)

#Add Dimensions of Interest
Todd_2016_Exp2a_Long <- Todd_2016_Exp2a_Long %>% rename(Sex = subsex)
Todd_2016_Exp2a_Long <- Todd_2016_Exp2a_Long %>% rename(Value = reaction_time)
Todd_2016_Exp2a_Long <- Todd_2016_Exp2a_Long %>% add_column(Paper = 'Todd_2016')
Todd_2016_Exp2a_Long <- Todd_2016_Exp2a_Long %>% add_column(Age = NA)
Todd_2016_Exp2a_Long <- Todd_2016_Exp2a_Long %>% add_column(StudyNumber = '2a')

#Create Consistency and Perspective Column
Todd_2016_Exp2a_Long<- Todd_2016_Exp2a_Long  %>% 
  mutate(Consistent = case_when(Condition == "OC_RT"  ~ 1,
                                Condition ==  "OI_RT"  ~ 0, 
                                Condition ==  "SC_RT"  ~1, 
                                Condition ==  "SI_RT"   ~0,
                                Condition == "OC_error"  ~ 1, 
                                Condition == "OI_error" ~ 0,
                                Condition == "SC_error" ~ 1 , 
                                Condition ==  "SI_error"  ~ 0))
#NOTE: Consistent Levels { 0 = Inconsistent, 1= Consistent, 2 = Subject Mean Across I+C}

Todd_2016_Exp2a_Long <- Todd_2016_Exp2a_Long %>% 
  mutate(PerspectiveSelf = case_when(Condition == "OC_RT"  ~ 0,
                                Condition ==  "OI_RT"  ~ 0, 
                                Condition ==  "SC_RT"  ~1, 
                                Condition ==  "SI_RT"   ~1,
                                Condition == "OC_error"  ~ 0, 
                                Condition == "OI_error" ~ 0,
                                Condition == "SC_error" ~ 1 , 
                                Condition ==  "SI_error"~ 1))
#NOTE: Consistent Levels { 0 = Other, 1= Self, 2 = Subject Mean Across Self+Other}

#Create RTorError Column 
Todd_2016_Exp2a_Long <- Todd_2016_Exp2a_Long  %>% 
  mutate(RTorError = case_when(Condition == "OC_RT"  ~ "RT",
                                Condition ==  "OI_RT"  ~ "RT", 
                                Condition ==  "SC_RT"  ~"RT", 
                                Condition ==  "SI_RT"   ~"RT",
                                Condition == "OC_error"  ~ "Error", 
                                Condition == "OI_error" ~ "Error",
                                Condition == "SC_error" ~ "Error" , 
                                Condition ==  "SI_error"  ~ "Error"))


#Recode Condition
Todd_2016_Exp2a_Long<- Todd_2016_Exp2a_Long %>% 
  mutate(emotion= case_when(emotion == 1 ~ "anxiety", 
                        emotion == 2  ~ "anger")) 
#Combine emotion and condition into one name 
Todd_2016_Exp2a_Long$Condition <- paste(Todd_2016_Exp2a_Long$Condition, Todd_2016_Exp2a_Long$emotion, sep="_")
Todd_2016_Exp2a_Long <- Todd_2016_Exp2a_Long[-c(3)] #Remove column so you end up with 10 clean columns

#Make Subjects and Condition Unique
Todd_2016_Exp2a_Long$Condition <- paste("Tod16_Ex2a", Todd_2016_Exp2a_Long$Condition,sep="_")
Todd_2016_Exp2a_Long$Subject <- paste(Todd_2016_Exp2a_Long$Subject,"Tod16_Ex2a",sep="_")

Todd_2016_Exp2a_Long <- Todd_2016_Exp2a_Long[, c(5,7,6,2,1,3,8,9,10,4)]
Todd_2016_Exp2a_Long$Condition<- factor(Todd_2016_Exp2a_Long$Condition)
```

Todd 2016 
###Exp2b
```{r}
Todd_2016_Exp2b <- read_sav("../data_files/Todd_2016_Exp2b_CH.sav")
Todd_2016_Exp2b <- Todd_2016_Exp2b %>% rename(filterdata = `filter_$`) #renamed because throwing weird errors with the dollar sign
Todd_2016_Exp2b  <- subset(Todd_2016_Exp2b , filterdata==1) #take only the data thats less than 30% error rate. This exclusion criteria was used by Todd too but the bad subjects were still included in the data we received. 

Todd_2016_Exp2b$Subject <- factor(Todd_2016_Exp2b$Subject)
Todd_2016_Exp2b <- Todd_2016_Exp2b[ , c("Subject", "subsex","emotion", "OC_RT","OI_RT", "SC_RT", "SI_RT", "OC_error","OI_error","SC_error","SI_error")]

Todd_2016_Exp2b <- Todd_2016_Exp2b %>% mutate(subsex = coalesce(subsex, 0)) # get rid of the NAs in the sex column. Note for the future that the 0 are NAs. 
Todd_2016_Exp2b<- Todd_2016_Exp2b  %>% 
  mutate(subsex= case_when(subsex == 0 ~ "Unreported",
                           subsex == 1 ~ "M", 
                        subsex == 2  ~ "F",
                        subsex == 3  ~ "F")) #three comes from the fact the paper reports 121 F 
Todd_2016_Exp2b$subsex <- factor(Todd_2016_Exp2b$subsex)

Todd_2016_Exp2b_Long <- gather(Todd_2016_Exp2b, Condition, reaction_time, OC_RT:SI_error, factor_key=TRUE)

#Add Dimensions of Interest
Todd_2016_Exp2b_Long  <- Todd_2016_Exp2b_Long  %>% rename(Sex = subsex)
Todd_2016_Exp2b_Long  <- Todd_2016_Exp2b_Long  %>% rename(Value = reaction_time)
Todd_2016_Exp2b_Long  <- Todd_2016_Exp2b_Long  %>% add_column(Paper = 'Todd_2016')
Todd_2016_Exp2b_Long  <- Todd_2016_Exp2b_Long  %>% add_column(Age = NA)
Todd_2016_Exp2b_Long  <- Todd_2016_Exp2b_Long  %>% add_column(StudyNumber = '2b')

#Create Consistency and Perspective Column
Todd_2016_Exp2b_Long <- Todd_2016_Exp2b_Long   %>% 
  mutate(Consistent = case_when(Condition == "OC_RT"  ~ 1,
                                Condition ==  "OI_RT"  ~ 0, 
                                Condition ==  "SC_RT"  ~1, 
                                Condition ==  "SI_RT"   ~0,
                                Condition == "OC_error"  ~ 1, 
                                Condition == "OI_error" ~ 0,
                                Condition == "SC_error" ~ 1 , 
                                Condition ==  "SI_error"  ~ 0))
#NOTE: Consistent Levels { 0 = Inconsistent, 1= Consistent, 2 = Subject Mean Across I+C}

Todd_2016_Exp2b_Long <- Todd_2016_Exp2b_Long  %>% 
  mutate(PerspectiveSelf = case_when(Condition == "OC_RT"  ~ 0,
                                Condition ==  "OI_RT"  ~ 0, 
                                Condition ==  "SC_RT"  ~1, 
                                Condition ==  "SI_RT"   ~1,
                                Condition == "OC_error"  ~ 0, 
                                Condition == "OI_error" ~ 0,
                                Condition == "SC_error" ~ 1 , 
                                Condition ==  "SI_error"~ 1))
#NOTE: Consistent Levels { 0 = Other, 1= Self, 2 = Subject Mean Across Self+Other}

#Create RTorError Column 
Todd_2016_Exp2b_Long  <- Todd_2016_Exp2b_Long   %>% 
  mutate(RTorError = case_when(Condition == "OC_RT"  ~ "RT",
                                Condition ==  "OI_RT"  ~ "RT", 
                                Condition ==  "SC_RT"  ~"RT", 
                                Condition ==  "SI_RT"   ~"RT",
                                Condition == "OC_error"  ~ "Error", 
                                Condition == "OI_error" ~ "Error",
                                Condition == "SC_error" ~ "Error" , 
                                Condition ==  "SI_error"  ~ "Error"))
#Recode Condition
Todd_2016_Exp2b_Long<- Todd_2016_Exp2b_Long %>% 
  mutate(emotion= case_when(emotion == 1 ~ "anxiety", 
                        emotion == 2  ~ "anger")) 
#Combine emotion and condition into one name 
Todd_2016_Exp2b_Long$Condition <- paste(Todd_2016_Exp2b_Long$Condition, Todd_2016_Exp2b_Long$emotion, sep="_")
Todd_2016_Exp2b_Long <- Todd_2016_Exp2b_Long[-c(3)] #Remove column so you end up with 10 clean columns

#Make Subjects and Condition Unique
Todd_2016_Exp2b_Long$Condition <- paste("Tod16_Ex2b", Todd_2016_Exp2b_Long$Condition,sep="_")
Todd_2016_Exp2b_Long$Subject <- paste(Todd_2016_Exp2b_Long$Subject,"Tod16_Ex2b",sep="_")

Todd_2016_Exp2b_Long <- Todd_2016_Exp2b_Long[, c(5,7,6,2,1,3,8,9,10,4)]
Todd_2016_Exp2b_Long$Condition <- factor(Todd_2016_Exp2b_Long$Condition)
```

Todd 2016 
###Combined
```{r}
Tod16 <- rbind(Todd_2016_Exp1_Long, Todd_2016_Exp2a_Long)
Tod16 <- rbind(Tod16, Todd_2016_Exp2b_Long)
Tod16$StudyNumber <- factor(Tod16$StudyNumber)
```

##Todd 2017 
###Study 1 Error Rate Only 
```{r}
Todd2017_Study1<- read_sav("../data_files/Todd2017_Study1_CH.sav")
Todd2017_Study1 <- Todd2017_Study1%>% rename(filterdata = `filter_$`) #renamed because throwing weird errors with the dollar sign
Todd2017_Study1 <- subset(Todd2017_Study1 , filterdata==1) #below chance performance removed 
colnames(Todd2017_Study1)

#trying to figure out the coding names 
Todd2017_Study1 <- Todd2017_Study1 %>% rename(timeout = Condition) 


Todd2017_Study1 <- Todd2017_Study1 %>% rename(Sex = gender) #rename to match naming convention 
# Todd2017_Study1 <- Todd2017_Study1 %>% rename(mean_error = total_errors) #rename to match naming convention.commented b/c we are not taking means anymore. 
Todd2017_Study1 <- Todd2017_Study1 %>% rename(Subject = ID) #rename to match naming convention
Todd2017_Study1$Subject <- factor(Todd2017_Study1$Subject) #need to factor subject before gathering
Todd2017_Study1 <- Todd2017_Study1[ , c("Subject", "timeout", "Sex", "OC_error","OI_error", "SC_error", "SI_error")]

#Recoding Sex Variable
Todd2017_Study1<- Todd2017_Study1 %>%  mutate(Sex = coalesce(Sex, 0)) #get rid of na's so that you can gather. replaced na's with 0 so you can recode
Todd2017_Study1<- Todd2017_Study1  %>% 
  mutate(Sex= case_when(Sex == 1 ~ "M", 
                        Sex == 2  ~ "F",
                        Sex == 0 ~ "Unreported")) #Recode the Sex variables. Discovered what 1 and 2 meant by doing analysis on the data and comparing with the number of male and female subjects reported in the paper. 
Todd2017_Study1$Sex <- factor(Todd2017_Study1$Sex)#need to factor subsex before gathering

#Wide to Long
Todd2017_Study1_Long <- gather(Todd2017_Study1, Condition, Value,OC_error:SI_error, factor_key=TRUE) 

#reference
colnames(Todd2017_Study1_Long)
levels(Todd2017_Study1_Long$Condition)

#Add Dimensions of Interest
Todd2017_Study1_Long <- Todd2017_Study1_Long %>% add_column(Paper = 'Todd_2017')
Todd2017_Study1_Long <- Todd2017_Study1_Long %>% add_column(Age = NA)
Todd2017_Study1_Long <- Todd2017_Study1_Long %>% add_column(RTorError = 'Error')
Todd2017_Study1_Long <- Todd2017_Study1_Long %>% add_column(StudyNumber = '1')

#Create Consistency and Perspective Column
Todd2017_Study1_Long<- Todd2017_Study1_Long  %>% 
  mutate(Consistent = case_when(Condition == "OC_error"  ~ 1, 
                                Condition == "OI_error" ~ 0,
                                Condition == "SC_error" ~ 1 , 
                                Condition ==  "SI_error"  ~ 0))
#NOTE: Consistent Levels { 0 = Inconsistent, 1= Consistent, 2 = Subject Mean Across I+C}
Todd2017_Study1_Long<- Todd2017_Study1_Long  %>% 
  mutate(PerspectiveSelf = case_when(Condition == "OC_error"  ~ 0, 
                                Condition == "OI_error" ~ 0,
                                Condition == "SC_error" ~ 1 , 
                                Condition ==  "SI_error"  ~ 1))
#NOTE: Consistent Levels { 0 = Other, 1= Self, 2 = Subject Mean Across Self+Other}

#Recode TimeOut Condition (Also discovered by analyses and comparing to original papers)
Todd2017_Study1_Long<- Todd2017_Study1_Long  %>% 
  mutate(timeout= case_when(timeout == 1 ~ 1200, 
                        timeout == 2  ~ 600))
#Combine Timeout and Condition into one name 
Todd2017_Study1_Long$Condition <- paste(Todd2017_Study1_Long$Condition,Todd2017_Study1_Long$timeout,sep="_")
Todd2017_Study1_Long <- Todd2017_Study1_Long[ -c(2) ] #Remove column so you end up with 10 clean columns

#Make Subjects and Condition Unique
Todd2017_Study1_Long$Condition <- paste("Tod17_Ex1", Todd2017_Study1_Long$Condition,sep="_")
Todd2017_Study1_Long$Subject <- paste(Todd2017_Study1_Long$Subject,"Tod17_Ex1",sep="_")


Todd2017_Study1_Long <- Todd2017_Study1_Long[, c(5,8,6,2,1,3,9,10,7,4)]
Todd2017_Study1_Long$Condition <- factor(Todd2017_Study1_Long$Condition)
```

Todd 2017 
###Study 2 Error Rate Only 
```{r}
Todd2017_Study2 <- read_sav("../data_files/Todd2017_Study2_CH.sav")
Todd2017_Study2 <- Todd2017_Study2%>% rename(filterdata = `filter_$`) #renamed because throwing weird errors with the dollar sign
Todd2017_Study2 <- subset(Todd2017_Study2 , filterdata==1) #below chance performance removed
colnames(Todd2017_Study2)

Todd2017_Study2 <- Todd2017_Study2 %>% rename(avatarcon = Condition) 

#rename to match naming convention 
Todd2017_Study2<- Todd2017_Study2 %>% rename(Sex = gender) 
# Todd2017_Study2 <- Todd2017_Study2 %>% rename(mean_error = total_errors) #Not doing mean errors anymore
Todd2017_Study2 <- Todd2017_Study2%>% rename(Subject = ID) 

Todd2017_Study2$Subject <- factor(Todd2017_Study2$Subject) #need to factor subject before gathering
Todd2017_Study2 <-Todd2017_Study2[ , c("Subject", "Sex","avatarcon", "OC_error","OI_error", "SC_error", "SI_error")]
Todd2017_Study2<- Todd2017_Study2 %>%  mutate(Sex = coalesce(Sex, 0)) #get rid of na's so that you can gather. replaced na's with 0
Todd2017_Study2<- Todd2017_Study2  %>% 
  mutate(Sex= case_when(Sex == 1 ~ "M", 
                        Sex == 2  ~ "F")) #Recode the Sex variables. Discovered what 1 and 2 meant by doing analysis on the data and comparing with the number of male and female subjects reported in the paper.
Todd2017_Study2$Sex <- factor(Todd2017_Study2$Sex)#need to factor subsex before gathering

#Wide to Long
Todd2017_Study2_Long <- gather(Todd2017_Study2, Condition, Value, OC_error:SI_error, factor_key=TRUE) 

#reference
colnames(Todd2017_Study2_Long)
levels(Todd2017_Study2_Long$Condition)

#Add Dimensions of Interest
Todd2017_Study2_Long<- Todd2017_Study2_Long %>% add_column(Paper = 'Todd_2017')
Todd2017_Study2_Long <- Todd2017_Study2_Long %>% add_column(Age = NA)
Todd2017_Study2_Long<- Todd2017_Study2_Long %>% add_column(RTorError = 'Error')
Todd2017_Study2_Long <- Todd2017_Study2_Long %>% add_column(StudyNumber = '2')


#Create Consistency and Perspective Column
Todd2017_Study2_Long<- Todd2017_Study2_Long  %>% 
  mutate(Consistent = case_when(Condition == "OC_error"  ~ 1, 
                                Condition == "OI_error" ~ 0,
                                Condition == "SC_error" ~ 1 , 
                                Condition ==  "SI_error"  ~ 0))
#NOTE: Consistent Levels { 0 = Inconsistent, 1= Consistent, 2 = Subject Mean Across I+C}
Todd2017_Study2_Long<- Todd2017_Study2_Long  %>% 
  mutate(PerspectiveSelf = case_when(Condition == "OC_error"  ~ 0, 
                                Condition == "OI_error" ~ 0,
                                Condition == "SC_error" ~ 1 , 
                                Condition ==  "SI_error"  ~ 1))
#NOTE: Consistent Levels { 0 = Other, 1= Self, 2 = Subject Mean Across Self+Other}


#Recode Avatar Condition (Also discovered by analyses and comparing to original papers)
Todd2017_Study2_Long<- Todd2017_Study2_Long  %>% 
  mutate(avatarcon= case_when(avatarcon == 1 ~ "social", 
                        avatarcon == 2  ~ "nonsocial"))
#Combine Timeout and Condition into one name 
Todd2017_Study2_Long$Condition <- paste(Todd2017_Study2_Long$Condition,Todd2017_Study2_Long$avatarcon,sep="_")
Todd2017_Study2_Long <- Todd2017_Study2_Long[ -c(3) ] #Remove column so you end up with 10 clean columns

#Make Subjects and Condition Unique
Todd2017_Study2_Long$Condition <- paste("Tod17_Ex2", Todd2017_Study2_Long$Condition,sep="_")
Todd2017_Study2_Long$Subject <- paste(Todd2017_Study2_Long$Subject,"Tod17_Ex1",sep="_")



Todd2017_Study2_Long <- Todd2017_Study2_Long[, c(5,8,6,2,1,3,9,10,7,4)]
Todd2017_Study2_Long$Condition <- factor(Todd2017_Study2_Long$Condition)
levels(Todd2017_Study2_Long$Condition)
```

Todd 2017
###Combined
```{r}
Tod17 <- rbind(Todd2017_Study1_Long, Todd2017_Study2_Long)
Tod17$StudyNumber <- factor(Tod17$StudyNumber)
```

##Simpson 2017 
###Ex1
```{r}
Simpson_2017_Exp1 <- read_sav("../data_files/Simpson_2017_Exp1.sav")

Simpson_2017_Exp1 <- Simpson_2017_Exp1 %>% rename(filterdata = 'filter_$')
Simpson_2017_Exp1 <- Simpson_2017_Exp1 %>% rename(Subject = ID)
Simpson_2017_Exp1 <- Simpson_2017_Exp1 %>% rename(Sex = sex)
Simpson_2017_Exp1 <- subset(Simpson_2017_Exp1, filterdata==1)

Simpson_2017_Exp1$Subject <- factor(Simpson_2017_Exp1$Subject)
Simpson_2017_Exp1 <- Simpson_2017_Exp1 %>% mutate(Sex = coalesce(Sex, 0)) # get rid of the NAs in the sex column. Note for the future that the 0 are NAs. 
Simpson_2017_Exp1<- Simpson_2017_Exp1  %>% 
  mutate(Sex= case_when(Sex == 1 ~ "M", 
                        Sex == 2  ~ "F", 
                        Sex == 0 ~ "Unreported")) #Recode the Sex variables. Discovered what 1 and 2 meant by doing analysis on the data and comparing with the number of male and female subjects reported in the paper.
Simpson_2017_Exp1$Sex <- factor(Simpson_2017_Exp1$Sex)

Simpson_2017_Exp1<- Simpson_2017_Exp1[ , c("Subject", "Sex", "HOC_RT","HOI_RT","HSC_RT", "HSI_RT","HOC_error", "HOI_error", "HSC_error","HSI_error", "COC_RT","COI_RT","CSC_RT","CSI_RT","COC_error", "COI_error","CSC_error","CSI_error")]
Simpson_2017_Exp1_Long <- gather(Simpson_2017_Exp1, Condition, Value, HOC_RT:CSI_error, factor_key=TRUE)

#reference
colnames(Simpson_2017_Exp1_Long)
levels(Simpson_2017_Exp1_Long$Condition)

#Add Dimensions of Interest
Simpson_2017_Exp1_Long  <- Simpson_2017_Exp1_Long  %>% add_column(Paper = 'Simpson_2017')
Simpson_2017_Exp1_Long  <- Simpson_2017_Exp1_Long  %>% add_column(Age = NA)
Simpson_2017_Exp1_Long  <- Simpson_2017_Exp1_Long  %>% add_column(StudyNumber = '1')

#Create Consistency and Perspective Column
Simpson_2017_Exp1_Long <- Simpson_2017_Exp1_Long  %>% 
  mutate(Consistent = case_when(Condition == "HOC_RT"  ~ 1,
                                Condition ==  "HOI_RT"    ~ 0, 
                                Condition ==  "HSC_RT"  ~1, 
                                Condition ==  "HSI_RT"   ~0,
                                Condition == "HOC_error"  ~ 1, 
                                Condition == "HOI_error" ~ 0,
                                Condition == "HSC_error" ~ 1 , 
                                Condition ==  "HSI_error"  ~ 0,
                                Condition ==  "COC_RT"   ~ 1, 
                                Condition ==  "COI_RT"   ~0, 
                                Condition ==  "CSC_RT"    ~1,
                                Condition == "CSI_RT"  ~ 0, 
                                Condition == "COC_error" ~ 1,
                                Condition == "COI_error" ~ 0 , 
                                Condition ==  "CSC_error"    ~ 1,
                                Condition == "CSI_error" ~ 0))
#NOTE: Consistent Levels { 0 = Inconsistent, 1= Consistent, 2 = Subject Mean Across I+C}

Simpson_2017_Exp1_Long <- Simpson_2017_Exp1_Long  %>% 
  mutate(PerspectiveSelf = case_when(Condition == "HOC_RT"  ~ 0,
                                Condition ==  "HOI_RT"    ~ 0, 
                                Condition ==  "HSC_RT"  ~1, 
                                Condition ==  "HSI_RT"   ~1,
                                Condition == "HOC_error"  ~ 0, 
                                Condition == "HOI_error" ~ 0,
                                Condition == "HSC_error" ~ 1 , 
                                Condition ==  "HSI_error"  ~ 1,
                                Condition ==  "COC_RT"   ~ 0, 
                                Condition ==  "COI_RT"   ~0, 
                                Condition ==  "CSC_RT"    ~1,
                                Condition == "CSI_RT"  ~ 1, 
                                Condition == "COC_error" ~ 0,
                                Condition == "COI_error" ~ 0 , 
                                Condition ==  "CSC_error"    ~ 1,
                                Condition == "CSI_error" ~ 1))

#NOTE: Consistent Levels { 0 = Other, 1= Self, 2 = Subject Mean Across Self+Other}

#Create RTorError Column 
Simpson_2017_Exp1_Long <- Simpson_2017_Exp1_Long  %>% 
  mutate(RTorError = case_when(Condition == "HOC_RT"  ~ "RT",
                                Condition ==  "HOI_RT"    ~ "RT", 
                                Condition ==  "HSC_RT"  ~"RT", 
                                Condition ==  "HSI_RT"   ~"RT",
                                Condition == "HOC_error"  ~ "Error", 
                                Condition == "HOI_error" ~ "Error",
                                Condition == "HSC_error" ~ "Error" , 
                                Condition ==  "HSI_error"  ~ "Error",
                                Condition ==  "COC_RT"   ~ "RT", 
                                Condition ==  "COI_RT"   ~"RT", 
                                Condition ==  "CSC_RT"    ~"RT",
                                Condition == "CSI_RT"  ~ "RT", 
                                Condition == "COC_error" ~ "Error",
                                Condition == "COI_error" ~ "Error" , 
                                Condition ==  "CSC_error"    ~ "Error",
                                Condition == "CSI_error" ~ "Error"))

#Make Subjects and Condition Unique
Simpson_2017_Exp1_Long$Subject <- paste(Simpson_2017_Exp1_Long$Subject,"Sim17_Ex1",sep="_")

Simpson_2017_Exp1_Long <- Simpson_2017_Exp1_Long[, c(5,7,6,2,1,3,8,9,10,4)]

Simpson_2017_Exp1_Long$Condition <- factor(Simpson_2017_Exp1_Long$Condition)
levels(Simpson_2017_Exp1_Long$Condition)
```

Simpson 2017 
###Exp2
```{r}
Simpson_2017_Exp2 <- read_sav("../data_files/Simpson_2017_Exp2.sav")

Simpson_2017_Exp2 <- Simpson_2017_Exp2 %>% rename(filterdata = 'filter_$')
Simpson_2017_Exp2 <- subset(Simpson_2017_Exp2, filterdata==1)
Simpson_2017_Exp2 <- Simpson_2017_Exp2 %>% rename(Subject = ID)
Simpson_2017_Exp2 <- Simpson_2017_Exp2 %>% rename(Sex = subsex)

Simpson_2017_Exp2$Subject <- factor(Simpson_2017_Exp2$Subject)
Simpson_2017_Exp2<- Simpson_2017_Exp2  %>% 
  mutate(Sex= case_when(Sex == 1 ~ "M", 
                        Sex == 2  ~ "F")) #Recode the Sex variables. Discovered what 1 and 2 meant by doing analysis on the data and comparing with the number of male and female subjects reported in the paper.
Simpson_2017_Exp2$Sex <- factor(Simpson_2017_Exp2$Sex)

colnames(Simpson_2017_Exp2)
Simpson_2017_Exp2<- Simpson_2017_Exp2[ ,c("Subject","Sex","ingroup_OC_RT","ingroup_OI_RT","ingroup_SC_RT",
                                          "ingroup_SI_RT","ingroup_OC_error","ingroup_OI_error","ingroup_SC_error","ingroup_SI_error",
                                          "outgroup_OC_RT","outgroup_OI_RT","outgroup_SC_RT","outgroup_SI_RT","outgroup_OC_error",
                                          "outgroup_OI_error","outgroup_SC_error", "outgroup_SI_error")]

Simpson_2017_Exp2_Long <- gather(Simpson_2017_Exp2, Condition, Value, ingroup_OC_RT:outgroup_SI_error, factor_key=TRUE)

#reference
colnames(Simpson_2017_Exp2_Long)
levels(Simpson_2017_Exp2_Long$Condition)

#Add Dimensions of Interest
Simpson_2017_Exp2_Long  <- Simpson_2017_Exp2_Long  %>% add_column(Paper = 'Simpson_2017')
Simpson_2017_Exp2_Long  <- Simpson_2017_Exp2_Long  %>% add_column(Age = NA)
Simpson_2017_Exp2_Long  <- Simpson_2017_Exp2_Long  %>% add_column(StudyNumber = '2')

#Create Consistency and Perspective Column
Simpson_2017_Exp2_Long <- Simpson_2017_Exp2_Long  %>% 
  mutate(Consistent = case_when(Condition == "mean_RT"  ~ 2, 
                                Condition == "mean_error" ~ 2, 
                                Condition == "ingroup_OC_RT"  ~ 1,
                                Condition ==  "ingroup_OI_RT"   ~ 0, 
                                Condition ==  "ingroup_SC_RT"  ~1, 
                                Condition ==  "ingroup_SI_RT"   ~0,
                                Condition == "ingroup_OC_error"  ~ 1, 
                                Condition == "ingroup_OI_error" ~ 0,
                                Condition == "ingroup_SC_error" ~ 1 , 
                                Condition ==  "ingroup_SI_error"  ~ 0,
                                Condition ==  "outgroup_OC_RT"   ~ 1, 
                                Condition ==  "outgroup_OI_RT"  ~0, 
                                Condition ==  "outgroup_SC_RT"   ~1,
                                Condition == "outgroup_SI_RT"~ 0, 
                                Condition == "outgroup_OC_error" ~ 1,
                                Condition == "outgroup_OI_error" ~ 0 , 
                                Condition ==  "outgroup_SC_error"    ~ 1,
                                Condition == "outgroup_SI_error" ~ 0))
#NOTE: Consistent Levels { 0 = Inconsistent, 1= Consistent, 2 = Subject Mean Across I+C}
Simpson_2017_Exp2_Long <- Simpson_2017_Exp2_Long  %>% 
  mutate(PerspectiveSelf = case_when(Condition == "mean_RT"  ~ 2, 
                                Condition == "mean_error" ~ 2, 
                                Condition == "ingroup_OC_RT"  ~ 0,
                                Condition ==  "ingroup_OI_RT"   ~ 0, 
                                Condition ==  "ingroup_SC_RT"  ~1, 
                                Condition ==  "ingroup_SI_RT"   ~1,
                                Condition == "ingroup_OC_error"  ~ 0, 
                                Condition == "ingroup_OI_error" ~ 0,
                                Condition == "ingroup_SC_error" ~ 1 , 
                                Condition ==  "ingroup_SI_error"  ~ 1,
                                Condition ==  "outgroup_OC_RT"   ~ 0, 
                                Condition ==  "outgroup_OI_RT"  ~0, 
                                Condition ==  "outgroup_SC_RT"   ~1,
                                Condition == "outgroup_SI_RT"~ 1, 
                                Condition == "outgroup_OC_error" ~ 0,
                                Condition == "outgroup_OI_error" ~ 0 , 
                                Condition ==  "outgroup_SC_error"    ~ 1,
                                Condition == "outgroup_SI_error" ~ 1))

#NOTE: Consistent Levels { 0 = Other, 1= Self, 2 = Subject Mean Across Self+Other}

#Create RTorError Column 
Simpson_2017_Exp2_Long <- Simpson_2017_Exp2_Long  %>% 
  mutate(RTorError = case_when(Condition == "mean_RT"  ~ "RT", 
                                Condition == "mean_error" ~ "Error", 
                                Condition == "ingroup_OC_RT"  ~ "RT",
                                Condition ==  "ingroup_OI_RT"   ~ "RT", 
                                Condition ==  "ingroup_SC_RT"  ~"RT", 
                                Condition ==  "ingroup_SI_RT"   ~"RT",
                                Condition == "ingroup_OC_error"  ~ "Error", 
                                Condition == "ingroup_OI_error" ~ "Error",
                                Condition == "ingroup_SC_error" ~ "Error" , 
                                Condition ==  "ingroup_SI_error"  ~ "Error",
                                Condition ==  "outgroup_OC_RT"   ~ "RT", 
                                Condition ==  "outgroup_OI_RT"  ~"RT", 
                                Condition ==  "outgroup_SC_RT"   ~"RT",
                                Condition == "outgroup_SI_RT"~ "RT", 
                                Condition == "outgroup_OC_error" ~ "Error",
                                Condition == "outgroup_OI_error" ~ "Error" , 
                                Condition ==  "outgroup_SC_error"    ~ "Error",
                                Condition == "outgroup_SI_error" ~ "Error"))


#Make Subjects and Condition Unique
Simpson_2017_Exp2_Long$Subject <- paste(Simpson_2017_Exp2_Long$Subject,"Sim17_Ex2",sep="_")

Simpson_2017_Exp2_Long <- Simpson_2017_Exp2_Long[, c(5,7,6,2,1,3,8,9,10,4)]
Simpson_2017_Exp2_Long$Condition <- factor(Simpson_2017_Exp2_Long$Condition)
```

Simpson 2017 
###Exp3 Error Only Data 
```{r}
Simpson_2017_Exp3 <- read_sav("../data_files/Simpson_2017_Exp3.sav") 
colnames(Simpson_2017_Exp3)
Simpson_2017_Exp3 <- Simpson_2017_Exp3 %>% rename(filterdata = 'filter_$')
Simpson_2017_Exp3 <- subset(Simpson_2017_Exp3, filterdata==1)
Simpson_2017_Exp3 <- Simpson_2017_Exp3 %>% rename(Subject = ID)
Simpson_2017_Exp3 <- Simpson_2017_Exp3 %>% rename(Sex = subsex)

Simpson_2017_Exp3$Subject <- factor(Simpson_2017_Exp3$Subject)
Simpson_2017_Exp3 <- Simpson_2017_Exp3 %>% mutate(Sex = coalesce(Sex, 0)) # get rid of the NAs in the sex column. Note for the future that the 0 are NAs. 
Simpson_2017_Exp3<- Simpson_2017_Exp3  %>% 
  mutate(Sex= case_when(Sex == 1 ~ "M", 
                        Sex == 2  ~ "F",
                        Sex == 0 ~"Unreported")) #Recode the Sex variables. Discovered what 1 and 2 meant by doing analysis on the data and comparing with the number of male and female subjects reported in the paper.
Simpson_2017_Exp3$Sex <- factor(Simpson_2017_Exp3$Sex)

Simpson_2017_Exp3<- Simpson_2017_Exp3[ , c("Subject", "Sex", "HOC_error", "HOI_error", "HSC_error","HSI_error","COC_error", "COI_error","CSC_error","CSI_error")]
Simpson_2017_Exp3_Long <- gather(Simpson_2017_Exp3, Condition, Value, HOC_error:CSI_error, factor_key=TRUE)

#reference
colnames(Simpson_2017_Exp3_Long)
levels(Simpson_2017_Exp3_Long$Condition)

#Add Dimensions of Interest
Simpson_2017_Exp3_Long  <- Simpson_2017_Exp3_Long  %>% add_column(Paper = 'Simpson_2017')
Simpson_2017_Exp3_Long  <- Simpson_2017_Exp3_Long  %>% add_column(Age = NA)
Simpson_2017_Exp3_Long  <- Simpson_2017_Exp3_Long  %>% add_column(StudyNumber = '3')

#Create Consistency and Perspective Column
Simpson_2017_Exp3_Long <- Simpson_2017_Exp3_Long  %>% 
  mutate(Consistent = case_when(Condition == "HOC_error"  ~ 1, 
                                Condition == "HOI_error" ~ 0,
                                Condition == "HSC_error" ~ 1 , 
                                Condition ==  "HSI_error"  ~ 0,
                                Condition == "COC_error" ~ 1,
                                Condition == "COI_error" ~ 0 , 
                                Condition ==  "CSC_error"    ~ 1,
                                Condition == "CSI_error" ~ 0))
#NOTE: Consistent Levels { 0 = Inconsistent, 1= Consistent, 2 = Subject Mean Across I+C}

Simpson_2017_Exp3_Long <- Simpson_2017_Exp3_Long  %>% 
  mutate(PerspectiveSelf = case_when(Condition == "HOC_error"  ~ 0, 
                                Condition == "HOI_error" ~ 0,
                                Condition == "HSC_error" ~ 1 , 
                                Condition ==  "HSI_error"  ~ 1, 
                                Condition == "COC_error" ~ 0,
                                Condition == "COI_error" ~ 0 , 
                                Condition ==  "CSC_error"    ~ 1,
                                Condition == "CSI_error" ~ 1))

#NOTE: Consistent Levels { 0 = Other, 1= Self, 2 = Subject Mean Across Self+Other}

#Create RTorError Column 
Simpson_2017_Exp3_Long <- Simpson_2017_Exp3_Long  %>% 
  mutate(RTorError = case_when(Condition == "HOC_error"  ~ "Error", 
                                Condition == "HOI_error" ~ "Error",
                                Condition == "HSC_error" ~ "Error" , 
                                Condition ==  "HSI_error"  ~ "Error",
                                Condition == "COC_error" ~ "Error",
                                Condition == "COI_error" ~ "Error" , 
                                Condition ==  "CSC_error"    ~ "Error",
                                Condition == "CSI_error" ~ "Error"))


#Make Subjects and Condition Unique
Simpson_2017_Exp3_Long$Condition <- paste("Sim17_Ex3", Simpson_2017_Exp3_Long$Condition, sep="_")
Simpson_2017_Exp3_Long$Subject <- paste(Simpson_2017_Exp3_Long$Subject,"Sim17_Ex3",  sep="_")


colnames(Simpson_2017_Exp3_Long)
Simpson_2017_Exp3_Long <- Simpson_2017_Exp3_Long[, c(5,7,6,2,1,3,8,9,10,4)]
Simpson_2017_Exp3_Long$Condition <- factor(Simpson_2017_Exp3_Long$Condition)
```

Simpson 2017 
###Combined
```{r}
Simp17 <- rbind(Simpson_2017_Exp1_Long, Simpson_2017_Exp2_Long)
Simp17 <- rbind(Simp17, Simpson_2017_Exp3_Long)
Simp17$StudyNumber <- factor(Simp17$StudyNumber)
```

##Gardner Hull 2018 
```{r}
GardnerH_2018_Exp1 <- read_sav("../data_files/Gardner_H_2018_QJEP_Experiment_1.sav")

#Convert the Error rate percentages into a decimal as is used in the other studies 
GardnerH_2018_Exp1$PE.Ar.incon <- GardnerH_2018_Exp1$PE.Ar.incon / 100
GardnerH_2018_Exp1$PE.Av.incon <- GardnerH_2018_Exp1$PE.Av.incon / 100
GardnerH_2018_Exp1$PE.Ar.con <- GardnerH_2018_Exp1$PE.Ar.con / 100
GardnerH_2018_Exp1$PE.Av.con <- GardnerH_2018_Exp1$PE.Av.con / 100

#rename 
GardnerH_2018_Exp1 <- GardnerH_2018_Exp1 %>% rename(filterdata = 'filter_$')
GardnerH_2018_Exp1 <- subset(GardnerH_2018_Exp1, filterdata==1)
GardnerH_2018_Exp1 <- GardnerH_2018_Exp1 %>% rename(Subject = subject)

GardnerH_2018_Exp1$Subject <- factor(GardnerH_2018_Exp1$Subject)
GardnerH_2018_Exp1$Sex <- factor(GardnerH_2018_Exp1$Sex)
GardnerH_2018_Exp1 <- GardnerH_2018_Exp1 %>%  mutate(Sex = recode(Sex, 'male' = "M", 
                           'female'= "F"))

GardnerH_2018_Exp1<- GardnerH_2018_Exp1[ , c("Subject", "Age", "Sex", "Arrow.incon", "Avatar.incon", "Arrow.con","Avatar.con","PE.Ar.incon","PE.Av.incon","PE.Ar.con", "PE.Av.con")]
GardnerH_2018_Exp1_Long <- gather(GardnerH_2018_Exp1, Condition, Value, Arrow.incon:PE.Av.con, factor_key=TRUE)

#reference
# colnames(GardnerH_2018_Exp1_Long)
# levels(GardnerH_2018_Exp1_Long$Condition)

#Add Dimensions of Interest
GardnerH_2018_Exp1_Long  <- GardnerH_2018_Exp1_Long  %>% add_column(Paper = 'Gardner_H_2018')
GardnerH_2018_Exp1_Long  <- GardnerH_2018_Exp1_Long  %>% add_column(StudyNumber = '1')


#Create Consistency and Perspective Column
GardnerH_2018_Exp1_Long <- GardnerH_2018_Exp1_Long  %>% 
  mutate(Consistent = case_when(Condition == "Arrow.incon" ~ 0, 
                                Condition == "Avatar.incon"  ~ 0, 
                                Condition == "Arrow.con" ~ 1,
                                Condition == "Avatar.con" ~ 1 , 
                                Condition ==  "PE.Ar.incon"  ~ 0,
                                Condition == "PE.Av.incon" ~ 0,
                                Condition == "PE.Ar.con" ~ 1 , 
                                Condition ==  "PE.Av.con"    ~ 1))
#NOTE: Consistent Levels { 0 = Inconsistent, 1= Consistent, 2 = Subject Mean Across I+C}

GardnerH_2018_Exp1_Long <- GardnerH_2018_Exp1_Long  %>%
  mutate(PerspectiveSelf = case_when(Condition == "Arrow.incon" ~ 1,
                                Condition == "Avatar.incon"  ~ 1,
                                Condition == "Arrow.con" ~ 1,
                                Condition == "Avatar.con" ~ 1 ,
                                Condition ==  "PE.Ar.incon"  ~ 1,
                                Condition == "PE.Av.incon" ~ 1,
                                Condition == "PE.Ar.con" ~ 1 ,
                                Condition ==  "PE.Av.con"    ~ 1))
#NOTE: Consistent Levels { 0 = Other, 1= Self, 2 = Subject Mean Across Self+Other}

#Create RTorError Column 
GardnerH_2018_Exp1_Long <- GardnerH_2018_Exp1_Long  %>% 
  mutate(RTorError = case_when(Condition == "Arrow.incon" ~ "RT", 
                                Condition == "Avatar.incon"  ~ "RT", 
                                Condition == "Arrow.con" ~ "RT",
                                Condition == "Avatar.con" ~ "RT" , 
                                Condition ==  "PE.Ar.incon"  ~ "Error",
                                Condition == "PE.Av.incon" ~ "Error",
                                Condition == "PE.Ar.con" ~ "Error" , 
                                Condition ==  "PE.Av.con"    ~ "Error"))
#Make Subjects and Condition Unique
GardnerH_2018_Exp1_Long$Subject <- paste(GardnerH_2018_Exp1_Long$Subject, "Gar18_Ex1", sep="_")


GardnerH_2018_Exp1_Long <- GardnerH_2018_Exp1_Long[, c(6,7,2,3,1,4,8,9,10,5)]
GarH18 <- GardnerH_2018_Exp1_Long
```

##Qureshi 2010 
###Exp1
```{r}
Qureshi_2010_Exp1 <- read.csv("../data_files/Qureshi_2010_Ep1.csv")

#Wide to Long
Qureshi_2010_Exp1$Subject <- factor(Qureshi_2010_Exp1$Subject)

colnames(Qureshi_2010_Exp1)
Qureshi_2010_Exp1 <- Qureshi_2010_Exp1 %>% 
   mutate(ER_COM_alone = ER_COM_alone / 13)
Qureshi_2010_Exp1 <- Qureshi_2010_Exp1 %>% 
   mutate(ER_COM_dual = ER_COM_dual / 13)

Qureshi_2010_Exp1 <- Qureshi_2010_Exp1 %>% 
   mutate(ER_CSM_alone = ER_CSM_alone / 13)
Qureshi_2010_Exp1 <- Qureshi_2010_Exp1 %>% 
   mutate(ER_CSM_dual = ER_CSM_dual / 13)

Qureshi_2010_Exp1 <- Qureshi_2010_Exp1 %>% 
   mutate(ER_IOM_alone = ER_IOM_alone / 13)
Qureshi_2010_Exp1 <- Qureshi_2010_Exp1 %>% 
   mutate(ER_IOM_dual = ER_IOM_dual / 13)

Qureshi_2010_Exp1 <- Qureshi_2010_Exp1 %>% 
   mutate(ER_ISM_alone = ER_ISM_alone / 13)
Qureshi_2010_Exp1 <- Qureshi_2010_Exp1 %>% 
   mutate(ER_ISM_dual = ER_ISM_dual / 13)

Qureshi_2010_Exp1_Long <- gather(Qureshi_2010_Exp1, Condition, Value, RT_COM_alone:ER_ISM_dual, factor_key=TRUE)
# Qureshi_2010_Exp1
# 
# #reference
# colnames(Qureshi_2010_Exp1_Long)
# levels(Qureshi_2010_Exp1_Long$Condition)

#Add Dimensions of Interest
Qureshi_2010_Exp1_Long <- Qureshi_2010_Exp1_Long  %>% add_column(Paper = 'Qureshi_2010')
Qureshi_2010_Exp1_Long <- Qureshi_2010_Exp1_Long  %>% add_column(Sex = NA)
Qureshi_2010_Exp1_Long <- Qureshi_2010_Exp1_Long  %>% add_column(Age = NA)
Qureshi_2010_Exp1_Long <- Qureshi_2010_Exp1_Long %>%  add_column(StudyNumber = '1')

Qureshi_2010_Exp1_Long <- Qureshi_2010_Exp1_Long  %>% 
  mutate(Consistent = case_when(Condition == "RT_COM_alone" ~ 1, 
                                Condition == "RT_COM_dual"  ~ 1, 
                                Condition == "RT_CSM_alone" ~ 1,
                                Condition ==  "RT_CSM_dual"  ~ 1, 
                                Condition ==  "RT_IOM_alone"  ~0, 
                                Condition ==  "RT_IOM_dual" ~0,
                                Condition == "RT_ISM_alone" ~ 0, 
                                Condition == "RT_ISM_dual"~ 0,
                                Condition == "ER_COM_alone"~ 1 , 
                                Condition ==  "ER_COM_dual"~ 1,
                                Condition == "ER_CSM_alone"~ 1, 
                                Condition ==  "ER_CSM_dual"~ 1,
                                 Condition == "ER_IOM_alone"~ 0 , 
                                Condition ==   "ER_IOM_dual" ~ 0,
                                Condition == "ER_ISM_alone"~ 0, 
                                Condition ==  "ER_ISM_dual" ~ 0))
                                
                                
Qureshi_2010_Exp1_Long <- Qureshi_2010_Exp1_Long  %>% 
  mutate(PerspectiveSelf = case_when(Condition == "RT_COM_alone" ~ 0, 
                                Condition == "RT_COM_dual"  ~ 0, 
                                Condition == "RT_CSM_alone" ~ 1,
                                Condition ==  "RT_CSM_dual"  ~ 1, 
                                Condition ==  "RT_IOM_alone"  ~0, 
                                Condition ==  "RT_IOM_dual" ~0,
                                Condition == "RT_ISM_alone" ~ 1, 
                                Condition == "RT_ISM_dual"~ 1,
                                Condition == "ER_COM_alone"~ 0 , 
                                Condition ==  "ER_COM_dual"~ 0,
                                Condition == "ER_CSM_alone"~ 1, 
                                Condition ==  "ER_CSM_dual"~ 1,
                                 Condition == "ER_IOM_alone"~ 0 , 
                                Condition ==   "ER_IOM_dual" ~ 0,
                                Condition == "ER_ISM_alone"~ 1, 
                                Condition ==  "ER_ISM_dual" ~ 1))
                                
Qureshi_2010_Exp1_Long <- Qureshi_2010_Exp1_Long  %>% 
  mutate(RTorError = case_when(Condition == "RT_COM_alone" ~ "RT", 
                                Condition == "RT_COM_dual"  ~ "RT", 
                                Condition == "RT_CSM_alone" ~ "RT",
                                Condition ==  "RT_CSM_dual"  ~ "RT", 
                                Condition ==  "RT_IOM_alone"  ~"RT", 
                                Condition ==  "RT_IOM_dual" ~"RT",
                                Condition == "RT_ISM_alone" ~ "RT", 
                                Condition == "RT_ISM_dual"~ "RT",
                                Condition == "ER_COM_alone"~ "Error" , 
                                Condition ==  "ER_COM_dual"~ "Error",
                                Condition == "ER_CSM_alone"~ "Error", 
                                Condition ==  "ER_CSM_dual"~ "Error",
                                 Condition == "ER_IOM_alone"~ "Error" , 
                                Condition ==   "ER_IOM_dual" ~ "Error",
                                Condition == "ER_ISM_alone"~ "Error", 
                                Condition ==  "ER_ISM_dual" ~ "Error"))        

#Make Subjects and Condition Unique
Qureshi_2010_Exp1_Long$Subject <- paste(Qureshi_2010_Exp1_Long$Subject, "Qur10_Ex1", sep="_")

Qureshi_2010_Exp1_Long <- Qureshi_2010_Exp1_Long[, c(4,7,6,5,1,2,8,9,10,3)]
# levels(Qureshi_2010_Exp1_Long$Condition)
Qur10 <- Qureshi_2010_Exp1_Long
```

##Ferguson 2017 
###Exp1 
```{r}
Ferguson_2017_Exp1 <- read.csv("../data_files/Ferguson_2017_Exp1.csv")
Ferguson_2017_Exp1 <- Ferguson_2017_Exp1[ , c("C_they_stick_accuracy","I_they_stick_accuracy","C_you_stick_accuracy",  
"I_you_stick_accuracy",   "C_they_switch_accuracy", "I_they_switch_accuracy", "C_you_switch_accuracy", 
"I_you_switch_accuracy",  "C_they_stick_RT" ,       "I_they_stick_RT",        "C_you_stick_RT",        
"I_you_stick_RT",         "C_they_switch_RT",       "I_they_switch_RT",       "C_you_switch_RT",       
"I_you_switch_RT",        "Subject"  )]

#Switch Accuracy to Error 
Ferguson_2017_Exp1$C_they_stick_accuracy <- 1 - Ferguson_2017_Exp1$C_they_stick_accuracy
Ferguson_2017_Exp1$I_they_stick_accuracy <- 1- Ferguson_2017_Exp1$I_they_stick_accuracy
Ferguson_2017_Exp1$C_you_stick_accuracy <- 1 - Ferguson_2017_Exp1$C_you_stick_accuracy
Ferguson_2017_Exp1$I_you_stick_accuracy <- 1- Ferguson_2017_Exp1$I_you_stick_accuracy
Ferguson_2017_Exp1$C_they_switch_accuracy <- 1 - Ferguson_2017_Exp1$C_they_switch_accuracy
Ferguson_2017_Exp1$I_they_switch_accuracy <- 1 - Ferguson_2017_Exp1$I_they_switch_accuracy
Ferguson_2017_Exp1$C_you_switch_accuracy <- 1 - Ferguson_2017_Exp1$C_you_switch_accuracy
Ferguson_2017_Exp1$I_you_switch_accuracy <- 1 - Ferguson_2017_Exp1$I_you_switch_accuracy

#rename Accuracy to Error
Ferguson_2017_Exp1 <- Ferguson_2017_Exp1 %>% rename(C_they_stick_error = C_they_stick_accuracy)
Ferguson_2017_Exp1 <- Ferguson_2017_Exp1 %>% rename(I_they_stick_error= I_they_stick_accuracy)
Ferguson_2017_Exp1 <- Ferguson_2017_Exp1 %>% rename(C_you_stick_error=C_you_stick_accuracy)
Ferguson_2017_Exp1 <- Ferguson_2017_Exp1 %>% rename(I_you_stick_error=I_you_stick_accuracy)
Ferguson_2017_Exp1 <- Ferguson_2017_Exp1 %>% rename(C_they_switch_error=C_they_switch_accuracy)
Ferguson_2017_Exp1 <- Ferguson_2017_Exp1 %>% rename(I_they_switch_error=I_they_switch_accuracy)
Ferguson_2017_Exp1 <- Ferguson_2017_Exp1 %>% rename(C_you_switch_error=C_you_switch_accuracy)
Ferguson_2017_Exp1 <- Ferguson_2017_Exp1 %>% rename(I_you_switch_error=I_you_switch_accuracy)

# colnames(Ferguson_2017_Exp1)
Ferguson_2017_Exp1$Subject <- factor(Ferguson_2017_Exp1$Subject)
Ferguson_2017_Exp1_Long <- gather(Ferguson_2017_Exp1, Condition, Value, C_they_stick_error:I_you_switch_RT, factor_key=TRUE)


#Add Dimensions of Interest
Ferguson_2017_Exp1_Long <- Ferguson_2017_Exp1_Long %>% add_column(Paper = 'Ferguson_2017')
Ferguson_2017_Exp1_Long <- Ferguson_2017_Exp1_Long  %>% add_column(Sex = NA)
Ferguson_2017_Exp1_Long <- Ferguson_2017_Exp1_Long  %>% add_column(Age = NA)
Ferguson_2017_Exp1_Long <- Ferguson_2017_Exp1_Long %>%  add_column(StudyNumber = '1')

#Create Consistent and PerspectiveSelf Columns
Ferguson_2017_Exp1_Long <- Ferguson_2017_Exp1_Long  %>% 
  mutate(Consistent = case_when(Condition == "C_they_stick_error" ~ 1, 
                                Condition == "I_they_stick_error"  ~ 0, 
                                Condition == "C_you_stick_error" ~ 1,
                                Condition ==  "I_you_stick_error"  ~ 0, 
                                Condition ==  "C_they_switch_error"  ~1, 
                                Condition ==  "I_they_switch_error" ~0,
                                Condition == "C_you_switch_error" ~ 1, 
                                Condition == "I_you_switch_error"~ 0,
                                Condition == "C_they_stick_RT"~ 1 , 
                                Condition == "I_they_stick_RT"~ 0, 
                                Condition ==  "C_you_stick_RT"~ 1,
                                Condition == "I_you_stick_RT"~ 0 , 
                                Condition ==   "C_they_switch_RT" ~ 1,
                                Condition == "I_they_switch_RT"~ 0, 
                                Condition ==  "C_you_switch_RT" ~ 1,
                                Condition ==  "I_you_switch_RT" ~ 0))
                                
Ferguson_2017_Exp1_Long <- Ferguson_2017_Exp1_Long  %>% 
  mutate(PerspectiveSelf = case_when(Condition == "C_they_stick_error" ~ 0, 
                                Condition == "I_they_stick_error"  ~ 0, 
                                Condition == "C_you_stick_error" ~ 1,
                                Condition ==  "I_you_stick_error"  ~ 1, 
                                Condition ==  "C_they_switch_error"  ~0, 
                                Condition ==  "I_they_switch_error" ~0,
                                Condition == "C_you_switch_error" ~ 1, 
                                Condition == "I_you_switch_error"~ 1,
                                Condition == "C_they_stick_RT"~ 0, 
                                Condition == "I_they_stick_RT"~ 0, 
                                Condition ==  "C_you_stick_RT"~ 1,
                                Condition == "I_you_stick_RT"~ 1, 
                                Condition ==   "C_they_switch_RT" ~ 0,
                                Condition == "I_they_switch_RT"~ 0, 
                                Condition ==  "C_you_switch_RT" ~ 1,
                                Condition ==  "I_you_switch_RT" ~ 1))

Ferguson_2017_Exp1_Long <- Ferguson_2017_Exp1_Long  %>% 
  mutate(RTorError= case_when(Condition == "C_they_stick_error" ~ "Error", 
                                Condition == "I_they_stick_error"  ~ "Error", 
                                Condition == "C_you_stick_error" ~ "Error",
                                Condition ==  "I_you_stick_error"  ~ "Error", 
                                Condition ==  "C_they_switch_error"  ~"Error", 
                                Condition ==  "I_they_switch_error" ~"Error",
                                Condition == "C_you_switch_error" ~ "Error", 
                                Condition == "I_you_switch_error"~ "Error",
                                Condition == "C_they_stick_RT"~ "RT", 
                                Condition == "I_they_stick_RT"~ "RT", 
                                Condition ==  "C_you_stick_RT"~ "RT",
                                Condition == "I_you_stick_RT"~ "RT", 
                                Condition ==   "C_they_switch_RT" ~ "RT",
                                Condition == "I_they_switch_RT"~ "RT", 
                                Condition ==  "C_you_switch_RT" ~ "RT",
                                Condition ==  "I_you_switch_RT" ~ "RT"))

#Make Subjects and Condition Unique
Ferguson_2017_Exp1_Long$Subject <- paste(Ferguson_2017_Exp1_Long$Subject, "Fer17_Ex1", sep="_")

Ferguson_2017_Exp1_Long <- Ferguson_2017_Exp1_Long[, c(4,7,6,5,1,2,8,9,10,3)]
# levels(Ferguson_2017_Exp1_Long$Condition)

Fer17 <- Ferguson_2017_Exp1_Long
```

##Ferguson 2018 
###Exp1
```{r}
Ferguson_2018_Exp1 <- read.csv("../data_files/Ferguson_2018_EXP1.csv")
Ferguson_2018_Exp1 <- Ferguson_2018_Exp1[-c(6)]

#Transform Accuracy to Error 
Ferguson_2018_Exp1$They.C_Error<- 1- Ferguson_2018_Exp1$They.C_Error
Ferguson_2018_Exp1$They.I_Error<- 1- Ferguson_2018_Exp1$They.I_Error
Ferguson_2018_Exp1$You.C_Error<- 1- Ferguson_2018_Exp1$You.C_Error
Ferguson_2018_Exp1$You.I_Error<- 1- Ferguson_2018_Exp1$You.I_Error

#rename to match convention 
Ferguson_2018_Exp1 <- Ferguson_2018_Exp1 %>% rename(Subject=Participant)

# colnames(Ferguson_2018_Exp1)
Ferguson_2018_Exp1$Subject <- factor(Ferguson_2018_Exp1$Subject)
Ferguson_2018_Exp1_Long <- gather(Ferguson_2018_Exp1, Condition, Value, They.C_Error:You.I_RT, factor_key=TRUE)


#Add Dimensions of Interest
Ferguson_2018_Exp1_Long <- Ferguson_2018_Exp1_Long %>% add_column(Paper = 'Ferguson_2018')
Ferguson_2018_Exp1_Long <- Ferguson_2018_Exp1_Long  %>% add_column(Sex = NA)
Ferguson_2018_Exp1_Long <- Ferguson_2018_Exp1_Long  %>% add_column(Age = NA)
Ferguson_2018_Exp1_Long <- Ferguson_2018_Exp1_Long %>%  add_column(StudyNumber = '1')


#Create Consistent and PerspectiveSelf Columns
Ferguson_2018_Exp1_Long <- Ferguson_2018_Exp1_Long  %>% 
  mutate(Consistent = case_when(Condition == "They.C_Error" ~ 1, 
                                Condition == "They.I_Error"  ~ 0, 
                                Condition == "You.C_Error" ~ 1,
                                Condition ==  "You.I_Error"  ~ 0, 
                                Condition ==  "They.C_RT"  ~1, 
                                Condition ==  "They.I_RT" ~0,
                                Condition == "You.C_RT" ~ 1, 
                                Condition == "You.I_RT"~ 0))
                                
Ferguson_2018_Exp1_Long <- Ferguson_2018_Exp1_Long  %>% 
  mutate(PerspectiveSelf = case_when(Condition == "They.C_Error" ~ 0, 
                                Condition == "They.I_Error"  ~ 0, 
                                Condition == "You.C_Error" ~ 1,
                                Condition ==  "You.I_Error"  ~ 1, 
                                Condition ==  "They.C_RT"  ~0, 
                                Condition ==  "They.I_RT" ~0,
                                Condition == "You.C_RT" ~ 1, 
                                Condition == "You.I_RT"~ 1))
                                
Ferguson_2018_Exp1_Long <- Ferguson_2018_Exp1_Long  %>% 
  mutate(RTorError = case_when(Condition == "They.C_Error" ~ "Error", 
                                Condition == "They.I_Error"  ~ "Error", 
                                Condition == "You.C_Error" ~ "Error",
                                Condition ==  "You.I_Error"  ~ "Error", 
                                Condition ==  "They.C_RT"  ~"RT", 
                                Condition ==  "They.I_RT" ~"RT",
                                Condition == "You.C_RT" ~ "RT", 
                                Condition == "You.I_RT"~ "RT"))
                                
Ferguson_2018_Exp1_Long$Condition <- paste(Ferguson_2018_Exp1_Long$Condition,Ferguson_2018_Exp1_Long$Avatar,sep=".")
#Make Subjects and Condition Unique
Ferguson_2018_Exp1_Long$Subject <- paste(Ferguson_2018_Exp1_Long$Subject, "Fer18_Ex1", sep="_")

Ferguson_2018_Exp1_Long <- Ferguson_2018_Exp1_Long[, c(5,8,7,6,1,3,9,10,11,4)]
Ferguson_2018_Exp1_Long$Condition <- factor(Ferguson_2018_Exp1_Long$Condition)
```

##Ferguson 2018 
###Exp2
```{r}
Ferguson_2018_Exp2 <- read.csv("../data_files/Ferguson_2018_EXP2.csv")

#Transform Accuracy to Error 
Ferguson_2018_Exp2$Adult_Other_Cons_Error <- 1- Ferguson_2018_Exp2$Adult_Other_Cons_Error 
Ferguson_2018_Exp2$Adult_Other_Incons_Error <- 1- Ferguson_2018_Exp2$Adult_Other_Incons_Error 
Ferguson_2018_Exp2$Adult_Self_Cons_Error <- 1- Ferguson_2018_Exp2$Adult_Self_Cons_Error 
Ferguson_2018_Exp2$Adult_Self_Incons_Error <- 1- Ferguson_2018_Exp2$Adult_Self_Incons_Error 
Ferguson_2018_Exp2$Child_Other_Cons_Error <- 1- Ferguson_2018_Exp2$Child_Other_Cons_Error 
Ferguson_2018_Exp2$Child_Other_Incons_Error <- 1- Ferguson_2018_Exp2$Child_Other_Incons_Error 
Ferguson_2018_Exp2$Child_Self_Cons_Error <- 1- Ferguson_2018_Exp2$Child_Self_Cons_Error 
Ferguson_2018_Exp2$Child_Self_Incons_Error <- 1- Ferguson_2018_Exp2$Child_Self_Incons_Error 
 
#rename to match convention 
Ferguson_2018_Exp2 <- Ferguson_2018_Exp2 %>% rename(Subject=Participant)
# colnames(Ferguson_2018_Exp2)
Ferguson_2018_Exp2$Subject <- factor(Ferguson_2018_Exp2$Subject)
Ferguson_2018_Exp2_Long <- gather(Ferguson_2018_Exp2, Condition, Value, Adult_Other_Cons_Error:Child_Self_Incons_RT, factor_key=TRUE)


#Add Dimensions of Interest
Ferguson_2018_Exp2_Long <- Ferguson_2018_Exp2_Long %>% add_column(Paper = 'Ferguson_2018')
Ferguson_2018_Exp2_Long <- Ferguson_2018_Exp2_Long  %>% add_column(Sex = NA)
Ferguson_2018_Exp2_Long <- Ferguson_2018_Exp2_Long  %>% add_column(Age = NA)
Ferguson_2018_Exp2_Long <- Ferguson_2018_Exp2_Long %>%  add_column(StudyNumber = '2')

#Create Consistent and PerspectiveSelf Columns
Ferguson_2018_Exp2_Long <- Ferguson_2018_Exp2_Long  %>% 
  mutate(Consistent = case_when(Condition == "Adult_Other_Cons_Error" ~ 1, 
                                Condition == "Adult_Other_Incons_Error"  ~ 0, 
                                Condition == "Adult_Self_Cons_Error"  ~ 1,
                                Condition ==  "Adult_Self_Incons_Error"  ~ 0, 
                                Condition ==  "Child_Other_Cons_Error"  ~1, 
                                Condition ==  "Child_Other_Incons_Error" ~0,
                                Condition == "Child_Self_Cons_Error"   ~ 1, 
                                Condition == "Child_Self_Incons_Error"~ 0,
                                Condition == "Adult_Other_Cons_RT"  ~ 1, 
                                Condition == "Adult_Other_Incons_RT"     ~ 0, 
                                Condition == "Adult_Self_Cons_RT"   ~ 1,
                                Condition ==   "Adult_Self_Incons_RT"  ~ 0, 
                                Condition ==  "Child_Other_Cons_RT"  ~1, 
                                Condition ==  "Child_Other_Incons_RT"   ~0,
                                Condition == "Child_Self_Cons_RT"    ~ 1, 
                                Condition == "Child_Self_Incons_RT" ~ 0))

Ferguson_2018_Exp2_Long <- Ferguson_2018_Exp2_Long  %>% 
  mutate(PerspectiveSelf = case_when(Condition == "Adult_Other_Cons_Error" ~ 0, 
                                Condition == "Adult_Other_Incons_Error"  ~ 0, 
                                Condition == "Adult_Self_Cons_Error"  ~ 1,
                                Condition ==  "Adult_Self_Incons_Error"  ~ 1, 
                                Condition ==  "Child_Other_Cons_Error"  ~0, 
                                Condition ==  "Child_Other_Incons_Error" ~0,
                                Condition == "Child_Self_Cons_Error"   ~ 1, 
                                Condition == "Child_Self_Incons_Error"~ 1,
                                Condition == "Adult_Other_Cons_RT"  ~ 0, 
                                Condition == "Adult_Other_Incons_RT"     ~ 0, 
                                Condition == "Adult_Self_Cons_RT"   ~ 1,
                                Condition ==   "Adult_Self_Incons_RT"  ~ 1, 
                                Condition ==  "Child_Other_Cons_RT"  ~0, 
                                Condition ==  "Child_Other_Incons_RT"   ~0,
                                Condition == "Child_Self_Cons_RT"    ~ 1, 
                                Condition == "Child_Self_Incons_RT" ~ 1))

Ferguson_2018_Exp2_Long <- Ferguson_2018_Exp2_Long  %>% 
  mutate(RTorError = case_when(Condition == "Adult_Other_Cons_Error" ~ "Error", 
                                Condition == "Adult_Other_Incons_Error"  ~ "Error", 
                                Condition == "Adult_Self_Cons_Error"  ~ "Error",
                                Condition ==  "Adult_Self_Incons_Error"  ~ "Error", 
                                Condition ==  "Child_Other_Cons_Error"  ~"Error", 
                                Condition ==  "Child_Other_Incons_Error" ~"Error",
                                Condition == "Child_Self_Cons_Error"   ~ "Error", 
                                Condition == "Child_Self_Incons_Error"~ "Error",
                                Condition == "Adult_Other_Cons_RT"  ~ "RT", 
                                Condition == "Adult_Other_Incons_RT"     ~ "RT", 
                                Condition == "Adult_Self_Cons_RT"   ~ "RT",
                                Condition ==   "Adult_Self_Incons_RT"  ~ "RT", 
                                Condition ==  "Child_Other_Cons_RT"  ~"RT", 
                                Condition ==  "Child_Other_Incons_RT"   ~"RT",
                                Condition == "Child_Self_Cons_RT"    ~ "RT", 
                                Condition == "Child_Self_Incons_RT" ~ "RT"))

#Make Subjects and Condition Unique
Ferguson_2018_Exp2_Long$Subject <- paste(Ferguson_2018_Exp2_Long$Subject, "Fer18_Ex2", sep="_")


Ferguson_2018_Exp2_Long <- Ferguson_2018_Exp2_Long[, c(5,8,7,6,1,3,9,10,11,4)]
Ferguson_2018_Exp2_Long$Condition <- factor(Ferguson_2018_Exp2_Long$Condition)
levels(Ferguson_2018_Exp2_Long$Condition)
```

Ferguson 2018
###Exp3
```{r}
Ferguson_2018_Exp3 <- read.csv("../data_files/Ferguson_2018_EXP3.csv")

#Transform Accuracy to Error 
Ferguson_2018_Exp3$Adult.C.Error <- 1- Ferguson_2018_Exp3$Adult.C.Error
Ferguson_2018_Exp3$Adult.I.Error <- 1- Ferguson_2018_Exp3$Adult.I.Error
Ferguson_2018_Exp3$Child.C.Error <- 1- Ferguson_2018_Exp3$Child.C.Error 
Ferguson_2018_Exp3$Child.I.Error <- 1- Ferguson_2018_Exp3$Child.I.Error

 
#rename to match convention 
Ferguson_2018_Exp3 <- Ferguson_2018_Exp3%>% rename(Subject=Participant)
# colnames(Ferguson_2018_Exp3)
Ferguson_2018_Exp3$Subject <- factor(Ferguson_2018_Exp3$Subject)
Ferguson_2018_Exp3_Long <- gather(Ferguson_2018_Exp3, Condition, Value, Adult.C.Error:Child.I.RT, factor_key=TRUE)


#Add Dimensions of Interest
Ferguson_2018_Exp3_Long <- Ferguson_2018_Exp3_Long %>% add_column(Paper = 'Ferguson_2018')
Ferguson_2018_Exp3_Long <- Ferguson_2018_Exp3_Long  %>% add_column(Sex = NA)
Ferguson_2018_Exp3_Long <- Ferguson_2018_Exp3_Long  %>% add_column(Age = NA)
Ferguson_2018_Exp3_Long <- Ferguson_2018_Exp3_Long %>%  add_column(StudyNumber = '3')

#Create Consistent and PerspectiveSelf Columns
Ferguson_2018_Exp3_Long <- Ferguson_2018_Exp3_Long  %>% 
  mutate(Consistent = case_when(Condition == "Adult.C.Error" ~ 1, 
                                Condition == "Adult.I.Error"  ~ 0,
                                Condition ==  "Child.C.Error"  ~1, 
                                Condition ==  "Child.I.Error" ~0,
                                Condition == "Adult.C.RT"  ~ 1, 
                                Condition == "Adult.I.RT"     ~ 0,
                                Condition ==  "Child.C.RT"  ~1, 
                                Condition ==  "Child.I.RT"   ~0))

#Steven said Exp3 is only Self Trials 
Ferguson_2018_Exp3_Long <- Ferguson_2018_Exp3_Long  %>%
  mutate(PerspectiveSelf = case_when(Condition == "Adult.C.Error" ~ 1,
                                Condition == "Adult.I.Error"  ~ 1,
                                Condition ==  "Child.C.Error"  ~1,
                                Condition ==  "Child.I.Error" ~1,
                                Condition == "Adult.C.RT"  ~ 1,
                                Condition == "Adult.I.RT"     ~ 1,
                                Condition ==  "Child.C.RT"  ~1,
                                Condition ==  "Child.I.RT"   ~1))

Ferguson_2018_Exp3_Long <- Ferguson_2018_Exp3_Long  %>%
  mutate(RTorError = case_when(Condition == "Adult.C.Error" ~ "Error", 
                                Condition == "Adult.I.Error"  ~ "Error",
                                Condition ==  "Child.C.Error"  ~"Error", 
                                Condition ==  "Child.I.Error" ~"Error",
                                Condition == "Adult.C.RT"  ~ "RT", 
                                Condition == "Adult.I.RT"     ~ "RT",
                                Condition ==  "Child.C.RT"  ~"RT", 
                                Condition ==  "Child.I.RT"   ~"RT"))
 
#Make Subjects and Condition Unique
Ferguson_2018_Exp3_Long$Subject <- paste(Ferguson_2018_Exp3_Long$Subject, "Fer18_Ex3", sep="_")

Ferguson_2018_Exp3_Long <- Ferguson_2018_Exp3_Long[, c(5,8,7,6,1,3,9,10,11,4)]
Ferguson_2018_Exp3_Long$Condition <- factor(Ferguson_2018_Exp3_Long$Condition)
levels(Ferguson_2018_Exp3_Long$Condition)
```

Ferguson 2018 
###Combined
```{r}
Fer18 <- rbind(Ferguson_2018_Exp1_Long,Ferguson_2018_Exp2_Long)
Fer18 <- rbind(Fer18, Ferguson_2018_Exp3_Long)
Fer18$StudyNumber <- factor(Fer18$StudyNumber)
levels(Fer18$StudyNumber)
```

##Capozzi 2014 
###RT
```{r}
#reminder to exclude two avatars for the final analysis 
Capozzi_2014_RT <- read.csv("../data_files/Capozzi_2014_Response_Times.csv")
colnames(Capozzi_2014_RT)[1] <- "Subject"
colnames(Capozzi_2014_RT)[2] <- "Age"
colnames(Capozzi_2014_RT)[3] <- "Sex"

Capozzi_2014_RT$Subject <- factor(Capozzi_2014_RT$Subject)
# colnames(Capozzi_2014_RT)
Capozzi_2014_RT_Long <- gather(Capozzi_2014_RT, Condition, Value,ACY_ONE_CENTER:SIN_TWO_OFFCENTER, factor_key=TRUE)

Capozzi_2014_RT_Long <- Capozzi_2014_RT_Long[, -c(4)] #remove avatar direction

Capozzi_2014_RT_Long$Condition<-factor(Capozzi_2014_RT_Long$Condition)
# levels(Capozzi_2014_RT_Long$Condition)

#Add Dimensions of Interest
Capozzi_2014_RT_Long <- Capozzi_2014_RT_Long %>% add_column(Paper = 'Capozzi_2014')
Capozzi_2014_RT_Long <- Capozzi_2014_RT_Long %>%  add_column(StudyNumber = '1')
Capozzi_2014_RT_Long <- Capozzi_2014_RT_Long %>%  add_column(RTorError = 'RT')
#Create Consistent and PerspectiveSelf Columns
Capozzi_2014_RT_Long <-Capozzi_2014_RT_Long   %>% 
  mutate(Consistent = case_when(Condition== "ACY_ONE_CENTER"~1,
    Condition== "AIY_ONE_CENTER"~0,
    Condition== "SCY_ONE_CENTER"~1,
    Condition== "SIY_ONE_CENTER"~0,   
    Condition== "ACY_ONE_OFFCENTER"~1,
    Condition== "AIY_ONE_OFFCENTER"~0,
    Condition== "SCY_ONE_OFFCENTER"~1,
    Condition== "SIY_ONE_OFFCENTER"~0,
    Condition== "ACY_TWO_CENTER"~1,  
    Condition== "AIY_TWO_CENTER" ~0,
    Condition== "SCY_TWO_CENTER"~1,
    Condition== "SIY_TWO_CENTER"~0,
    Condition== "ACY_TWO_OFFCENTER"~1,
    Condition== "AIY_TWO_OFFCENTER"~0,
    Condition== "SCY_TWO_OFFCENTER"~1,
    Condition== "SIY_TWO_OFFCENTER"~0,
    Condition== "ACN_ONE_CENTER"~1,
    Condition== "AIN_ONE_CENTER" ~0,
    Condition== "SCN_ONE_CENTER"~1,
    Condition== "SIN_ONE_CENTER"~0,   
    Condition== "ACN_ONE_OFFCENTER"~1,
    Condition== "AIN_ONE_OFFCENTER"~0,
    Condition== "SCN_ONE_OFFCENTER"~1,
    Condition== "SIN_ONE_OFFCENTER"~0,
    Condition== "ACN_TWO_CENTER"~1,
    Condition== "AIN_TWO_CENTER"~0,
    Condition== "SCN_TWO_CENTER"~1,
    Condition== "SIN_TWO_CENTER"~0,
    Condition== "ACN_TWO_OFFCENTER"~1,
    Condition== "AIN_TWO_OFFCENTER"~0,
    Condition== "SCN_TWO_OFFCENTER"~1,
    Condition== "SIN_TWO_OFFCENTER"~0))

Capozzi_2014_RT_Long <-Capozzi_2014_RT_Long   %>% 
  mutate(PerspectiveSelf = case_when(Condition== "ACY_ONE_CENTER"~0,
    Condition== "AIY_ONE_CENTER"~0,
    Condition== "SCY_ONE_CENTER"~1,
    Condition== "SIY_ONE_CENTER"~1,   
    Condition== "ACY_ONE_OFFCENTER"~0,
    Condition== "AIY_ONE_OFFCENTER"~0,
    Condition== "SCY_ONE_OFFCENTER"~1,
    Condition== "SIY_ONE_OFFCENTER"~1,
    Condition== "ACY_TWO_CENTER"~0,  
    Condition== "AIY_TWO_CENTER" ~0,
    Condition== "SCY_TWO_CENTER"~1,
    Condition== "SIY_TWO_CENTER"~1,
    Condition== "ACY_TWO_OFFCENTER"~0,
    Condition== "AIY_TWO_OFFCENTER"~0,
    Condition== "SCY_TWO_OFFCENTER"~1,
    Condition== "SIY_TWO_OFFCENTER"~1,
    Condition== "ACN_ONE_CENTER"~0,
    Condition== "AIN_ONE_CENTER" ~0,
    Condition== "SCN_ONE_CENTER"~1,
    Condition== "SIN_ONE_CENTER"~1,   
    Condition== "ACN_ONE_OFFCENTER"~0,
    Condition== "AIN_ONE_OFFCENTER"~0,
    Condition== "SCN_ONE_OFFCENTER"~1,
    Condition== "SIN_ONE_OFFCENTER"~1,
    Condition== "ACN_TWO_CENTER"~0,
    Condition== "AIN_TWO_CENTER"~0,
    Condition== "SCN_TWO_CENTER"~1,
    Condition== "SIN_TWO_CENTER"~1,
    Condition== "ACN_TWO_OFFCENTER"~0,
    Condition== "AIN_TWO_OFFCENTER"~0,
    Condition== "SCN_TWO_OFFCENTER"~1,
    Condition== "SIN_TWO_OFFCENTER"~1))

#Make Subjects and Condition Unique
Capozzi_2014_RT_Long$Subject <- paste(Capozzi_2014_RT_Long$Subject, "Cap14_Ex1", sep="_")

Capozzi_2014_RT_Long  <- Capozzi_2014_RT_Long[, c(6,7,2,3,1,4,9,10,8, 5)]
Capozzi_2014_RT_Long$Condition <- factor(Capozzi_2014_RT_Long$Condition)
```

Capozzi 2014 
###Error (transformed from Accuracy)
```{r}
Capozzi_2014_ER <- read.csv("../data_files/Capozzi_2014_Accuracy.csv")
colnames(Capozzi_2014_ER)[1] <- "Subject" #rename to match convention
colnames(Capozzi_2014_ER)[2] <- "Age"
colnames(Capozzi_2014_ER)[3] <- "Sex"

Capozzi_2014_ER$Subject <- factor(Capozzi_2014_ER$Subject)

Capozzi_2014_ER_Long <- gather(Capozzi_2014_ER, Condition, Value, ACY_ONE_CENTER:SIN_TWO_OFFCENTER, factor_key=TRUE)

#Swap Accuracy for Error
Capozzi_2014_ER_Long$Value <- 1- Capozzi_2014_ER_Long$Value
Capozzi_2014_ER_Long <- Capozzi_2014_ER_Long[, -c(4)] #remove avatar direction.


#Add Dimensions of Interest
Capozzi_2014_ER_Long<- Capozzi_2014_ER_Long %>% add_column(Paper = 'Capozzi_2014')
Capozzi_2014_ER_Long <- Capozzi_2014_ER_Long %>%  add_column(StudyNumber = '1')
Capozzi_2014_ER_Long <- Capozzi_2014_ER_Long %>%  add_column(RTorError = 'Error')
#Create Consistent and PerspectiveSelf Columns
Capozzi_2014_ER_Long <-Capozzi_2014_ER_Long   %>% 
  mutate(Consistent = case_when(Condition== "ACY_ONE_CENTER"~1,
    Condition== "AIY_ONE_CENTER"~0,
    Condition== "SCY_ONE_CENTER"~1,
    Condition== "SIY_ONE_CENTER"~0,   
    Condition== "ACY_ONE_OFFCENTER"~1,
    Condition== "AIY_ONE_OFFCENTER"~0,
    Condition== "SCY_ONE_OFFCENTER"~1,
    Condition== "SIY_ONE_OFFCENTER"~0,
    Condition== "ACY_TWO_CENTER"~1,  
    Condition== "AIY_TWO_CENTER" ~0,
    Condition== "SCY_TWO_CENTER"~1,
    Condition== "SIY_TWO_CENTER"~0,
    Condition== "ACY_TWO_OFFCENTER"~1,
    Condition== "AIY_TWO_OFFCENTER"~0,
    Condition== "SCY_TWO_OFFCENTER"~1,
    Condition== "SIY_TWO_OFFCENTER"~0,
    Condition== "ACN_ONE_CENTER"~1,
    Condition== "AIN_ONE_CENTER" ~0,
    Condition== "SCN_ONE_CENTER"~1,
    Condition== "SIN_ONE_CENTER"~0,   
    Condition== "ACN_ONE_OFFCENTER"~1,
    Condition== "AIN_ONE_OFFCENTER"~0,
    Condition== "SCN_ONE_OFFCENTER"~1,
    Condition== "SIN_ONE_OFFCENTER"~0,
    Condition== "ACN_TWO_CENTER"~1,
    Condition== "AIN_TWO_CENTER"~0,
    Condition== "SCN_TWO_CENTER"~1,
    Condition== "SIN_TWO_CENTER"~0,
    Condition== "ACN_TWO_OFFCENTER"~1,
    Condition== "AIN_TWO_OFFCENTER"~0,
    Condition== "SCN_TWO_OFFCENTER"~1,
    Condition== "SIN_TWO_OFFCENTER"~0))

Capozzi_2014_ER_Long <-Capozzi_2014_ER_Long   %>% 
  mutate(PerspectiveSelf = case_when(Condition== "ACY_ONE_CENTER"~0,
    Condition== "AIY_ONE_CENTER"~0,
    Condition== "SCY_ONE_CENTER"~1,
    Condition== "SIY_ONE_CENTER"~1,   
    Condition== "ACY_ONE_OFFCENTER"~0,
    Condition== "AIY_ONE_OFFCENTER"~0,
    Condition== "SCY_ONE_OFFCENTER"~1,
    Condition== "SIY_ONE_OFFCENTER"~1,
    Condition== "ACY_TWO_CENTER"~0,  
    Condition== "AIY_TWO_CENTER" ~0,
    Condition== "SCY_TWO_CENTER"~1,
    Condition== "SIY_TWO_CENTER"~1,
    Condition== "ACY_TWO_OFFCENTER"~0,
    Condition== "AIY_TWO_OFFCENTER"~0,
    Condition== "SCY_TWO_OFFCENTER"~1,
    Condition== "SIY_TWO_OFFCENTER"~1,
    Condition== "ACN_ONE_CENTER"~0,
    Condition== "AIN_ONE_CENTER" ~0,
    Condition== "SCN_ONE_CENTER"~1,
    Condition== "SIN_ONE_CENTER"~1,   
    Condition== "ACN_ONE_OFFCENTER"~0,
    Condition== "AIN_ONE_OFFCENTER"~0,
    Condition== "SCN_ONE_OFFCENTER"~1,
    Condition== "SIN_ONE_OFFCENTER"~1,
    Condition== "ACN_TWO_CENTER"~0,
    Condition== "AIN_TWO_CENTER"~0,
    Condition== "SCN_TWO_CENTER"~1,
    Condition== "SIN_TWO_CENTER"~1,
    Condition== "ACN_TWO_OFFCENTER"~0,
    Condition== "AIN_TWO_OFFCENTER"~0,
    Condition== "SCN_TWO_OFFCENTER"~1,
    Condition== "SIN_TWO_OFFCENTER"~1))

#Make Subjects and Condition Unique
Capozzi_2014_ER_Long$Subject <- paste(Capozzi_2014_ER_Long $Subject, "Cap14_Ex1", sep="_")

Capozzi_2014_ER_Long  <- Capozzi_2014_ER_Long[, c(6,7,2,3,1,4,9,10,8, 5)]
Capozzi_2014_ER_Long$Condition <- factor(Capozzi_2014_ER_Long$Condition)
```

###Capozzi 2014 Combined 
```{r}
Cap14 <- rbind(Capozzi_2014_RT_Long,Capozzi_2014_ER_Long) %>% 
  filter(!grepl("TWO", Condition)) %>% 
  mutate(
    Matching = case_when(
      substr(Condition, 3, 3) == 'N' ~ "mismatch",
      substr(Condition, 3, 3) == 'Y' ~ "match"
    )
  )
```

##Santiesteban 2014 
###Exp1
```{r}
Santiesteban_2014_Exp1 <- read_sav("../data_files/Santiesteban_2014_Avatar_Arrow_Data_Exp1.sav")
Santiesteban_2014_Exp1 <- read_sav("../data_files/Santiesteban_2014_Avatar_Arrow_Data_Exp1.sav")
Santiesteban_2014_Exp2 <- read_sav("../data_files/Santiesteban_2014_Exp2.sav")

colnames(Santiesteban_2014_Exp2)
Santiesteban_2014_Exp2 <- Santiesteban_2014_Exp2[,c(1:4,6,9,11,14,16,18,20 )] #Take only the yes responses

#rename to match convention 
Santiesteban_2014_Exp2 <- Santiesteban_2014_Exp2%>% rename(Subject=SubNo)
Santiesteban_2014_Exp2 <- Santiesteban_2014_Exp2%>% rename(Sex=Gender)
Santiesteban_2014_Exp2$Subject <- factor(Santiesteban_2014_Exp2$Subject)
Santiesteban_2014_Exp2_Long <- gather(Santiesteban_2014_Exp2, Condition, Value, Self_Con_Y:E_Self2_Inc_Y, factor_key=TRUE)

#Recode Condition Names to include avatar/arrow (supplemental provided in email from Santiesteban)
# levels(Santiesteban_2014_Exp2_Long$Condition)
Santiesteban_2014_Exp2_Long <- Santiesteban_2014_Exp2_Long %>%  
  mutate(Condition = recode(Condition,  
                      "Self_Con_Y" =  "Self_Con_Y_Avatar",
                      "Self_Inc_Y"=   "Self_Inc_Y_Avatar",
                      "Self2_Con_Y"=   "Self2_Con_Y_Arrow",
                      "Self2_Inc_Y" =  "Self2_Inc_Y_Arrow",
                      "E_Self_Con_Y" = "E_Self_Con_Y_Avatar", 
                      "E_Self_Inc_Y"  ="E_Self_Inc_Y_Avatar",
                      "E_Self2_Con_Y"="E_Self2_Con_Y_Arrow",
                      "E_Self2_Inc_Y" ="E_Self2_Inc_Y_Arrow" ))
#Recode Sex
Santiesteban_2014_Exp2_Long <- Santiesteban_2014_Exp2_Long %>%  
  mutate(Sex = case_when(Sex == 1 ~ "F", 
                         Sex == 2~  "M"))

#Add Dimensions of Interest
Santiesteban_2014_Exp2_Long <- Santiesteban_2014_Exp2_Long %>% add_column(Paper = 'Santiesteban_2014')
Santiesteban_2014_Exp2_Long <- Santiesteban_2014_Exp2_Long %>%  add_column(StudyNumber = '2')

#Create Consistent and PerspectiveSelf Columns
Santiesteban_2014_Exp2_Long <-Santiesteban_2014_Exp2_Long   %>% 
  mutate(Consistent = case_when( 
                      Condition==  "Self_Con_Y_Avatar" ~1,
                      Condition==  "Self_Inc_Y_Avatar"~0,
                      Condition==  "Self2_Con_Y_Arrow"~1,
                      Condition==   "Self2_Inc_Y_Arrow"~0,
                      Condition==   "E_Self_Con_Y_Avatar"~1, 
                      Condition==  "E_Self_Inc_Y_Avatar"~0,
                      Condition==  "E_Self2_Con_Y_Arrow"~1,
                      Condition== "E_Self2_Inc_Y_Arrow" ~0))

#Doublecheck you only get self trials in this experiment
Santiesteban_2014_Exp2_Long <-Santiesteban_2014_Exp2_Long   %>% 
  mutate(PerspectiveSelf = case_when( 
                      Condition==  "Self_Con_Y_Avatar" ~1,
                      Condition==  "Self_Inc_Y_Avatar"~1,
                      Condition==  "Self2_Con_Y_Arrow"~1,
                      Condition==   "Self2_Inc_Y_Arrow"~1,
                      Condition==   "E_Self_Con_Y_Avatar"~1, 
                      Condition==  "E_Self_Inc_Y_Avatar"~1,
                      Condition==  "E_Self2_Con_Y_Arrow"~1,
                      Condition== "E_Self2_Inc_Y_Arrow" ~1))

Santiesteban_2014_Exp2_Long <-Santiesteban_2014_Exp2_Long   %>% 
  mutate(RTorError = case_when( 
                      Condition==  "Self_Con_Y_Avatar" ~"RT",
                      Condition==  "Self_Inc_Y_Avatar"~"RT",
                      Condition==  "Self2_Con_Y_Arrow"~"RT",
                      Condition==   "Self2_Inc_Y_Arrow"~"RT",
                      Condition==   "E_Self_Con_Y_Avatar"~"Error", 
                      Condition==  "E_Self_Inc_Y_Avatar"~"Error",
                      Condition==  "E_Self2_Con_Y_Arrow"~"Error",
                      Condition== "E_Self2_Inc_Y_Arrow" ~"Error"))
  
  
#Make Subjects and Condition Unique
Santiesteban_2014_Exp2_Long$Subject <- paste(Santiesteban_2014_Exp2_Long$Subject, "San14_Ex2", sep="_")
Santiesteban_2014_Exp2_Long$Condition <- paste( "San14_Ex2", Santiesteban_2014_Exp2_Long$Condition, sep="_")

Santiesteban_2014_Exp2_Long <- Santiesteban_2014_Exp2_Long[, c(6,7,2,3,1,4,8,9,10,5)]
Santiesteban_2014_Exp2_Long$Condition <- factor(Santiesteban_2014_Exp2_Long$Condition)
```

###Exp2
```{r}
Santiesteban_2014_Exp2 <- read_sav("../data_files/Santiesteban_2014_Exp2.sav")

colnames(Santiesteban_2014_Exp2)
Santiesteban_2014_Exp2 <- Santiesteban_2014_Exp2[,c(1:4,6,9,11,14,16,18,20 )] #Take only the yes responses

#rename to match convention 
Santiesteban_2014_Exp2 <- Santiesteban_2014_Exp2%>% rename(Subject=SubNo)
Santiesteban_2014_Exp2 <- Santiesteban_2014_Exp2%>% rename(Sex=Gender)
Santiesteban_2014_Exp2$Subject <- factor(Santiesteban_2014_Exp2$Subject)
Santiesteban_2014_Exp2_Long <- gather(Santiesteban_2014_Exp2, Condition, Value, Self_Con_Y:E_Self2_Inc_Y, factor_key=TRUE)

#Recode Condition Names to include avatar/arrow (supplemental provided in email from Santiesteban)
# levels(Santiesteban_2014_Exp2_Long$Condition)
Santiesteban_2014_Exp2_Long <- Santiesteban_2014_Exp2_Long %>%  
  mutate(Condition = recode(Condition,  
                      "Self_Con_Y" =  "Self_Con_Y_Avatar",
                      "Self_Inc_Y"=   "Self_Inc_Y_Avatar",
                      "Self2_Con_Y"=   "Self2_Con_Y_Arrow",
                      "Self2_Inc_Y" =  "Self2_Inc_Y_Arrow",
                      "E_Self_Con_Y" = "E_Self_Con_Y_Avatar", 
                      "E_Self_Inc_Y"  ="E_Self_Inc_Y_Avatar",
                      "E_Self2_Con_Y"="E_Self2_Con_Y_Arrow",
                      "E_Self2_Inc_Y" ="E_Self2_Inc_Y_Arrow" ))
#Recode Sex
Santiesteban_2014_Exp2_Long <- Santiesteban_2014_Exp2_Long %>%  
  mutate(Sex = case_when(Sex == 1 ~ "F", 
                         Sex == 2~  "M"))

#Add Dimensions of Interest
Santiesteban_2014_Exp2_Long <- Santiesteban_2014_Exp2_Long %>% add_column(Paper = 'Santiesteban_2014')
Santiesteban_2014_Exp2_Long <- Santiesteban_2014_Exp2_Long %>%  add_column(StudyNumber = '2')

#Create Consistent and PerspectiveSelf Columns
Santiesteban_2014_Exp2_Long <-Santiesteban_2014_Exp2_Long   %>% 
  mutate(Consistent = case_when( 
                      Condition==  "Self_Con_Y_Avatar" ~1,
                      Condition==  "Self_Inc_Y_Avatar"~0,
                      Condition==  "Self2_Con_Y_Arrow"~1,
                      Condition==   "Self2_Inc_Y_Arrow"~0,
                      Condition==   "E_Self_Con_Y_Avatar"~1, 
                      Condition==  "E_Self_Inc_Y_Avatar"~0,
                      Condition==  "E_Self2_Con_Y_Arrow"~1,
                      Condition== "E_Self2_Inc_Y_Arrow" ~0))

#Doublecheck you only get self trials in this experiment
Santiesteban_2014_Exp2_Long <-Santiesteban_2014_Exp2_Long   %>% 
  mutate(PerspectiveSelf = case_when( 
                      Condition==  "Self_Con_Y_Avatar" ~1,
                      Condition==  "Self_Inc_Y_Avatar"~1,
                      Condition==  "Self2_Con_Y_Arrow"~1,
                      Condition==   "Self2_Inc_Y_Arrow"~1,
                      Condition==   "E_Self_Con_Y_Avatar"~1, 
                      Condition==  "E_Self_Inc_Y_Avatar"~1,
                      Condition==  "E_Self2_Con_Y_Arrow"~1,
                      Condition== "E_Self2_Inc_Y_Arrow" ~1))

Santiesteban_2014_Exp2_Long <-Santiesteban_2014_Exp2_Long   %>% 
  mutate(RTorError = case_when( 
                      Condition==  "Self_Con_Y_Avatar" ~"RT",
                      Condition==  "Self_Inc_Y_Avatar"~"RT",
                      Condition==  "Self2_Con_Y_Arrow"~"RT",
                      Condition==   "Self2_Inc_Y_Arrow"~"RT",
                      Condition==   "E_Self_Con_Y_Avatar"~"Error", 
                      Condition==  "E_Self_Inc_Y_Avatar"~"Error",
                      Condition==  "E_Self2_Con_Y_Arrow"~"Error",
                      Condition== "E_Self2_Inc_Y_Arrow" ~"Error"))
  
  
#Make Subjects and Condition Unique
Santiesteban_2014_Exp2_Long$Subject <- paste(Santiesteban_2014_Exp2_Long$Subject, "San14_Ex2", sep="_")
Santiesteban_2014_Exp2_Long$Condition <- paste( "San14_Ex2", Santiesteban_2014_Exp2_Long$Condition, sep="_")

Santiesteban_2014_Exp2_Long <- Santiesteban_2014_Exp2_Long[, c(6,7,2,3,1,4,8,9,10,5)]
Santiesteban_2014_Exp2_Long$Condition <- factor(Santiesteban_2014_Exp2_Long$Condition)
```

##O'Grady 2020
Exp1 
```{r}

#This is all data cleaning code that can be found here (https://osf.io/dbz39/)
df <- read.delim("../data_files/ogrady_2020_experiment-1_data.csv", header = FALSE, sep = "",  col.names = c("computerNumber", "participantNumber", "condition", 
                                 "yesSide", "participantGender", "trialNumber", 
                                 "blockNumber", "flipped", "BA", "A", "FA", "J", 
                                 "FS", "S", "BS", "image", "ballsInScene", 
                                 "avGender", "genderMatch", "consistency",
                                 "losPers", "dirPers", "yesNo", 
                                 "numberShown", "perspective", 
                                 "responseKey", "response", "RT", "correctResponse", 
                                 "correct"))

#clean up scenario vectors
df <- df %>%
  mutate(BA = recode(BA, "[0," = 0, "[1," = 1, "[2," = 2),
         A = recode(A, "0," = 0, "1," = 1),
         FA = recode(FA, "0," = 0, "1," = 1, "2," = 2),
         J = recode(J, "0," = 0, "1," = 1, "2," = 2),
         FS = recode(FS, "0," = 0, "1," = 1, "2," = 2),
         S = recode(S, "0," = 0, "1," = 1),
         BS = recode(BS, "0]" = 0, "1]" = 1, "2]" = 2))


#Remove participants with computer failure
df <- subset(df, participantNumber != "23" & participantNumber != "110")
#Remove either outlier participant (207) or extra participant recorded to potentially replace them (235)
df <- subset(df, participantNumber != "235")

#rename factor levels for Stim, Flipped, GenderMatch
df$flipped <- factor(df$flipped, labels = c("not flipped", "flipped"))
df$genderMatch <- factor(df$genderMatch, labels = c("not match", "match"))

#Remove test trials
df <- subset(df, blockNumber != "test")
#Recode blockNumber as numeric, starting at 0
df$blockNumber <- as.numeric(paste(df$blockNumber))
df$blockNumber <- df$blockNumber - 1

#Remove 0 balls scenes
df <- subset(df, ballsInScene > 0)
#Recode ballsInScene to run from 0 (i.e. 1 ball) to 3 (i.e. 4 balls)
df$ballsInScene <- df$ballsInScene - 1

#Remove NA responses, make note of how many, cross-check
naResponses <- subset(df, is.na(response))
numberNAs <- length(naResponses$response)
#check how many NA responses there were
print(numberNAs) 
#create new df with no NA responses
noNAdf <- subset(df, !is.na(response))
#cross-check: is the number of NA responses the same as 
#the length differences between the two dataframes?
lendf <- length(df$response)
lenNoNAdf <- length(noNAdf$response)
print(numberNAs == lendf-lenNoNAdf)
#print the proportion of NA responses
proportionNAs <- numberNAs/lendf*100
print(proportionNAs)

#Remove impossible responses, make note of how many, cross-check
impossResponses <- subset(noNAdf, RT < 100)
numberImpossResponses <- length(impossResponses$RT)
#check number of impossible responses
print(numberImpossResponses) 
#create new df with no imposs responses
noIRdf <- subset(noNAdf, RT > 100)
#cross-check: is the number of incorrect responses the same
#as the length difference between the two dataframes?
lenNoIRdf <- length(noIRdf$RT)
print(numberImpossResponses == lenNoNAdf-lenNoIRdf)
#print the proportion of incorrect responses
proportionImpossResponses <- numberImpossResponses/lenNoIRdf*100
print(proportionImpossResponses) # 0.0233141%

#Remove errors, cross-check, make note of proportion
errors <- subset (noIRdf, correct == 0)
numberErrors <- length(errors$correct)
#check number of errors
print(numberErrors)
#create new df with no errors
df <- subset(noIRdf, correct == 1)
#cross-check and print proportion
lenNoIRdf <- length(noIRdf$correct)
lendf <- length(df$correct)
print(numberErrors == lenNoIRdf-lendf)
proportionErrors <- numberErrors/lenNoIRdf*100
print(proportionErrors)

#set computerNumber as factor
df$computerNumber <- factor(df$computerNumber)
#change ballsInScene to factor
df$ballsInScene <- factor(df$ballsInScene)
#add a column with participant numbers as factors rather than numeric:
#numeric needed for plots, factor needed for models
df$participantNumFac <- factor(df$participantNumber)
```

#Merge Data (C. Holland)
```{r}
OgradySant2 <- read.csv("../data_files/OgradySant2-cleaned.csv")

CH_Data <- rbind(Fur16,Tod17)
CH_Data <- rbind(CH_Data, Tod16 )
CH_Data <-rbind(CH_Data, Simp17)
CH_Data <-rbind(CH_Data, GarH18)
CH_Data <-rbind(CH_Data, Qur10)
CH_Data <-rbind(CH_Data, Fer17)
CH_Data <-rbind(CH_Data, Fer18)
CH_Data <-rbind(CH_Data, OgradySant2)
CH_Data<- CH_Data %>%  add_column(Matching = 'match')
CH_Data <-rbind(CH_Data, Cap14)

CH_Data$Paper <- factor(CH_Data$Paper)

CH_classifications <- classifications %>% 
  filter(
    Paper == "OGrady_2020" |
    Paper == "Furlanetto_2016" |
    Paper == "Todd_2016" |
    Paper == "Todd_2017" |
    Paper == "Simpson_2017" |
    Paper == "Gardner_Hull_2018" |
    Paper == "Qureshi_2010" |
    Paper == "Ferguson_2017" |
    Paper == "Ferguson_2018" |
    Paper == "Capozzi_2014"|
    Paper == "Santiesteban_2014")

CH_final_data <- merge(CH_Data, CH_classifications, by.x="Condition", by.y = "Condition", all=TRUE )

```


#Merge S.Shin and C.Holland files
```{r}
SS_final <- Samson_final

CH_final <- CH_final_data %>% 
  select(-Paper.y) %>% 
  mutate(Matching = "match") %>%
  dplyr::rename(Paper = Paper.x)

merged_df <- rbind(SS_final, CH_final)

#Make coding consistent
merged_df <- merged_df %>% 
  mutate(
    Sex = case_when(
      Sex == 'M' ~ 'Male',
      Sex == 'F' ~ 'Female',
      Sex == 'Unreported' ~ NA_character_,
      TRUE ~ Sex
    )
  )

#Delete Unneeded Columns
merged_df <- merged_df %>% 
  select(
    -X,
    -Experiment.Number,
    -SOA,
    -N,
    -Consistent
    )
```

#Write out csv
```{r}
write.csv(merged_df, "../Samson_data_final.csv")
```








